ASP.NET Core???????3???? ?????e??????[???]
???????????? ???????[ 2016/6/7 14:47:45 ] ????????ASP.NET Core ???ù???
????????????????????????????????????ConfigurationBuilder??????Configuration???????????????????????????????????????????e????????????????????Options????????????μ??????????????????????????Options??????????????????Options?????????????????Options????????????????????????
???????ConfigurationBinder
??????????????????????????в?λ??????????????????Options?????????????????????????????????????Options????????????????λ????????????Options???????????????????沢??б??????????Options??????????????????????????????????????????????????????????Options???????????????????飬??????????????ConfigurationBinder????????????????????????????????????????????
????ConfigurationBinder??????????????“Microsoft.Extensions.Configuration.Binder” ?е????????????????????????????NuGet???????????????????????????????????????????Bind??????Get?????С??????????????????????Щ???????IConfiguration?????????????
????1: public static class ConfigurationBinder
????2: {
????3: public static void Bind(this IConfiguration configuration?? object instance);
????4:
????5: public static object Get(this IConfiguration configuration?? Type type);
????6: public static object Get(this IConfiguration configuration?? Type type?? string key);
????7: public static T Get<T>(this IConfiguration configuration);
????8: public static T Get<T>(this IConfiguration configuration?? T defaultValue);
????9: public static T Get<T>(this IConfiguration configuration?? string key);
????10: public static T Get<T>(this IConfiguration configuration?? string key?? T defaultValue);
????11: }
??????????????Bind?????????Configuration??????????????????????Get???????????????????????????type?????????????????????????????????????Configuration????????????????????????????????t??в???key??Get????????????????????????Key??????????y??
????ConfigurationBinder??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Щ????????????????????????????????????????
????????????????????
????????????????????????????????KeyValuePair <string??string >???????????e????????????????????????????ν??????????????????????????????綨????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????e????????????????????????????ConfigurationSection??Value???????????????????????????????????л???????????Int32??Double?????????????????????????????????μ?????????????
????1: IConfiguration configuration = new ConfigurationBuilder()
????2: .Add(new MemoryConfigurationProvider(new Dictionary<string?? string>
????3: {
????4: ["foo"] = "abc"??
????5: ["bar"] = "123"??
????6: ["baz"] = "3.14"
????7: })).Build();
????8:
????9: Debug.Assert(configuration.GetSection("foo").Get<string>() == "abc");
????10: Debug.Assert(configuration.Get<int>("bar") == 123);
????11: Debug.Assert(configuration.Get<double>("bar") == 3.14);
???????????????????????????????2???????????????????????????????TypeConverter?????????????????????????????????????????????????????????????????????????????????????????????????????Point?????????????TypeConverterAttribute????????????????????????????????TypeConverter??PointTypeConverter????????????e????????????????????????????????????????????Point????
1: [TypeConverter(typeof(PointTypeConverter))]
2: public class Point
3: {
4: public double X { get; set; }
5: public double Y { get; set; }
6: }
7:
8: public class PointTypeConverter : TypeConverter
9: {
10: public override object ConvertFrom(ITypeDescriptorContext context?? CultureInfo culture?? object value)
11: {
12: string[] split = value.ToString().Split('??');
13: return new Point
14: {
15: X = double.Parse(split[0].TrimStart('('))??
16: Y = double.Parse(split[1].TrimStart(')'))
17: };
18: }
19: }
20:
21: IConfiguration configuration = new ConfigurationBuilder().Add(new MemoryConfigurationProvider(new Dictionary<string?? string>
22: {
23: ["point"] = "(1.2??3.4)"
24: })).Build();
25: Debug.Assert(configuration.Get<Point>("point").X == 1.2);
26: Debug.Assert(configuration.Get<Point>("point").Y == 3.4);
??????????????????????
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????е????????????????????????????????????????ɡ??????????????????????????????????????????????????????·????????????Key???????
1: public class Profile
2: {
3: public Gender Gender { get; set; }
4: public int Age { get; set; }
5: public ContactInfo ContactInfo { get; set; }
6: }
7:
8: public class ContactInfo
9: {
10: public string Email { get; set; }
11: public string PhoneNo { get; set; }
12: }
13:
14: public enum Gender
15: {
16: Male??
17: Female
18: }
????5?????????????????????????????????????????????Profile?????????е??????????Gender??Age??ContactInfo??????????????????????????????????ContactInfo????????????????Email??PhoneNo???????????????????綽?????????????Profile?????????????????????????????
??????
???·???
??????????????????
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