ASP.NET Core???????3???? ?????e??????[???]
???????????? ???????[ 2016/6/7 14:47:45 ] ????????ASP.NET Core ???ù???
???????????????????????????????????????e?????????????????????????????????ConfigurationBuilder??????????????MemoryConfigurationProvider????????????3??????????????????????????????????ConfigurationBuilder????????Configuration???????????ConfigurationSection??Get??????Key?“Profiles”?????y??????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: }
?????????????e??????????????????????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????????????????????????????????????????????????????????????????????????????????????????????????y??Key????????????????Key?????????y??Key????????????????????????Dictionary<string?? Profile >?????????????????????????????????????????????Key????????????????????????????????????????“Foo”??“Bar”??“Baz”??????????Key????
??????????????????????????????????????????????????????????????????С??????????????Dictionary<string?? Profile>???????????????????????????????????????????????????????????
???????????????????????????????????????e?????????????????????????????????ConfigurationBuilder??????????????MemoryConfigurationProvider??????????????????????????????????????????????????ConfigurationBuilder????????Configuration???????????ConfigurationSection??Get??????Key?“Profiles”?????y??????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: }
?????????????e??????????????????????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
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11