我们依然通过一个简单的实例来演示针对集合的配置绑定。如下面的代码片段所示,我们创建了一个ConfigurationBuilder对象并为之添加了一个MemoryConfigurationProvider,后者按照如表3所示的结构提供了原始的配置数据。我们利用这个ConfigurationBuilder对象创建的Configuration对象并调用这个ConfigurationSection的Get方法将Key为“Profiles”的配置节绑定为一个List<Profile>对象。
1: public class Program
2: {
3:     public static void Main(string[] args)
4:     {
5:         IConfiguration configuration = new ConfigurationBuilder().Add(new MemoryConfigurationProvider(new Dictionary<string, string>
6:             {
7:                 ["Profiles:0:Gender"]       = "Male",
8:                 ["Profiles:0:Age"]      = "18",
9:                 ["Profiles:0:ContactInfo:Email"]   = "foo@gmail.com",
10:                 ["Profiles:0:ContactInfo:PhoneNo"]     = "123",
11:
12:                 ["Profiles:1:Gender"]       = "Male",
13:                 ["Profiles:1:Age"]      = "25",
14:                 ["Profiles:1:ContactInfo:Email"]       = "bar@gmail.com",
15:                 ["Profiles:1:ContactInfo:PhoneNo"]     = "456",
16:
17:                 ["Profiles:2:Gender"]       = "Female",
18:                 ["Profiles:2:Age"]      = "40",
19:                 ["Profiles:2:ContactInfo:Email"]       = "baz@gmail.com",
20:                 ["Profiles:2:ContactInfo:PhoneNo"]     = "789",
21:             })).Build();
22:
23:         IEnumerable<Profile> profiles = configuration.Get<List<Profile>>("Profiles");
24:         foreach (Profile profile in profiles)
25:         {
26:
27:             Console.WriteLine("{0,-10}:{1}", "Gender", profile.Gender);
28:             Console.WriteLine("{0,-10}:{1}", "Age", profile.Age);
29:             Console.WriteLine("{0,-10}:{1}", "Email", profile.ContactInfo.Email);
30:             Console.WriteLine("{0,-10}:{1} ", "PhoneNo", profile.ContactInfo.PhoneNo);
31:         }
32:     }
33: }
  为了验证配置绑定是否成功,我们终将这个绑定的List<Profile>对象的每个元素的相关信息打印出来,该程序执行之后会在控制台上产生如下所示的输出结果。
  1: Gender    :Male
  2: Age       :18
  3: Email     :foo@gmail.com
  4: PhoneNo   :123
  5:
  6: Gender    :Male
  7: Age       :25
  8: Email     :bar@gmail.com
  9: PhoneNo   :456
  10:
  11: Gender    :Female
  12: Age       :40
  13: Email     :baz@gmail.com
  14: PhoneNo   :789
  五、绑定字典

  8image字典可以视为元素类型为键值对的集合,两者在配置树上的表示非常相似,它们之间的不同之处在于前者采用索引作为集合元素所在配置节的Key,后者直接将键值对的Key直接作为配置节的Key。举个简单的例子,我们通过一个Dictionary<string, Profile >对象来表示多个用户的个人信息,并且将用户名作为这个字典的Key,那么这个字典对象的配置树将具有如右图所示的结构(“Foo”、“Bar”和“Baz”表示用户名的Key)。

  表示集合与字典的配置树在结构上基本类似,所以反映在基于数据字典的物理结构上也大同小异。对于右图表示的Dictionary<string, Profile>对象,构造该对象的所有原子配置数据可以通过包含如下元素的数据字典来提供。
  我们依然通过一个简单的实例来演示针对字典的配置绑定。如下面的代码片段所示,我们创建了一个ConfigurationBuilder对象并为之添加了一个MemoryConfigurationProvider,后者按照如左边表格所示的结构提供了原始的配置数据。我们利用这个ConfigurationBuilder对象创建的Configuration对象并调用这个ConfigurationSection的Get方法将Key为“Profiles”的配置节绑定为一个Dictionary<string, Profile>对象。
1: public class Program
2: {
3:     public static void Main(string[] args)
4:     {
5:         IConfiguration configuration = new ConfigurationBuilder().Add(new MemoryConfigurationProvider(new Dictionary<string, string>
6:             {
7:                 ["Profile:Foo:Gender"]               = "Male",
8:                 ["Profile:Foo:Age"]                  = "18",
9:                 ["Profile:Foo:ContactInfo:Email"]    = "foo@gmail.com",
10:                 ["Profile:Foo:ContactInfo:PhoneNo"]  = "123",
11:
12:                 ["Profile:Bar:Gender"]               = "Male",
13:                 ["Profile:Bar:Age"]                  = "25",
14:                 ["Profile:Bar:ContactInfo:Email"]    = "bar@gmail.com",
15:                 ["Profile:Bar:ContactInfo:PhoneNo"]  = "456",
16:
17:                 ["Profile:Baz:Gender"]               = "Female",
18:                 ["Profile:Baz:Age"]                  = "40",
19:                 ["Profile:Baz:ContactInfo:Email"]    = "baz@gmail.com",
20:                 ["Profile:Baz:ContactInfo:PhoneNo"]  = "789",
21:             })).Build();
22:
23:         Dictionary<string, Profile> profiles = configuration.Get<Dictionary<string, Profile>>("Profile");
24:         foreach (var item in profiles)
25:         {
26:
27:             Console.WriteLine("{0,-10}:{1}", "Name", item.Key );
28:             Console.WriteLine("{0,-10}:{1}", "Gender", item.Value.Gender);
29:             Console.WriteLine("{0,-10}:{1}", "Age", item.Value.Age);
30:             Console.WriteLine("{0,-10}:{1}", "Email", item.Value.ContactInfo.Email);
31:             Console.WriteLine("{0,-10}:{1} ", "PhoneNo", item.Value.ContactInfo.PhoneNo);
32:         }
33:     }
34: }
  为了验证配置绑定是否成功,我们终将这个绑定的Dictionary<string, Profile>对象的每个元素的相关信息打印出来,该程序执行之后会在控制台上产生如下所示的输出结果。
1: Name      :Foo
2: Gender    :Male
3: Age       :18
4: Email     :foo@gmail.com
5: PhoneNo   :123
6:
7: Name      :Bar
8: Gender    :Male
9: Age       :25
10: Email     :bar@gmail.com
11: PhoneNo   :456
12:
13: Name      :Baz
14: Gender    :Female
15: Age       :40
16: Email     :baz@gmail.com
17: PhoneNo   :789