?????????????????????FormatSettings??????????????????????????????á???????????????????????????????DateTime??CurrencyDecimal????????????/?????????????????á?FormatSettings???????????????????IConfiguration?????????????????????????????????б???????????????????????????????????????Configuration??“?????y?”???????????y????????GetSection??????á?
????1: public class FormatSettings
????2: {
????3:     public DateTimeFormatSettings            DateTime { get; set; }
????4:     public CurrencyDecimalFormatSettings     CurrencyDecimal { get; set; }
????5:
????6:     public FormatSettings(IConfiguration configuration)
????7:     {
????8:         this.DateTime = new DateTimeFormatSettings(configuration.GetSection("DateTime"));
????9:         this.CurrencyDecimal = new CurrencyDecimalFormatSettings(configuration.GetSection("CurrencyDecimal"));
????10:     }
????11: }
???????????????????????У?????????????MemoryConfigurationProvider????????????????????????????????????????????????????KeyValuePair<string?? string>??????????????????????洢????????????λ????ν???????????????????????????Configuration??????????????????MemoryConfigurationProvider????????????????洢?????“???????”???????????Configuration???????????????????·?????Key????????????????????????????????????????????MemoryConfigurationProvider????????????????????????????????????С?
1: class Program
2: {
3:     static void Main(string[] args)
4:     {
5:         Dictionary<string?? string> source = new Dictionary<string?? string>
6:         {
7:             ["Format:DateTime:LongDatePattern"]     = "dddd?? MMMM d?? yyyy"??
8:             ["Format:DateTime:LongTimePattern"]     = "h:mm:ss tt"??
9:             ["Format:DateTime:ShortDatePattern"]     = "M/d/yyyy"??
10:             ["Format:DateTime:ShortTimePattern"]     = "h:mm tt"??
11:
12:             ["Format:CurrencyDecimal:Digits"]     = "2"??
13:             ["Format:CurrencyDecimal:Symbol"]     = "$"??
14:         };
15:         IConfiguration configuration = new ConfigurationBuilder()
16:                 .Add(new MemoryConfigurationProvider(source))
17:                 .Build();
18:
19:         FormatSettings settings = new FormatSettings(configuration.GetSection("Format"));
20:         Console.WriteLine("DateTime:");
21:         Console.WriteLine(" {0??-16}: {1}"?? "LongDatePattern"?? settings.DateTime.LongDatePattern);
22:         Console.WriteLine(" {0??-16}: {1}"?? "LongTimePattern"?? settings.DateTime.LongTimePattern);
23:         Console.WriteLine(" {0??-16}: {1}"?? "ShortDatePattern"?? settings.DateTime.ShortDatePattern);
24:         Console.WriteLine(" {0??-16}: {1} "?? "ShortTimePattern"?? settings.DateTime.ShortTimePattern);
25:
26:         Console.WriteLine("CurrencyDecimal:");
27:         Console.WriteLine(" {0??-16}: {1}"?? "Digits"?? settings.CurrencyDecimal.Digits);
28:         Console.WriteLine(" {0??-16}: {1}"?? "Symbol"?? settings.CurrencyDecimal.Symbol);
29:     }
30: }
??????????????????????????MemoryConfigurationProvider?????????????????6???????????????????????????????????????λ???ν????????Key???????????????????????????y??????????е?·????·??????e???“??”?????з?????????????????????????????????????????
????1: DateTime:
????2:         LongDatePattern : dddd?? MMMM d?? yyyy
????3:         LongTimePattern : h:mm:ss tt
????4:         ShortDatePattern: M/d/yyyy
????5:         ShortTimePattern: h:mm tt
????6:
????7: CurrencyDecimal:
????8:         Digits          : 2
????9:         Symbol          : $
???????????????????????????
????????????????????????У????????????????????????????????????????????????????????????????????????????????DateTimeFormatSettings??CurrencyDecimalSettings??FormatSettings??????????????????????Option????????????????????Option??????Щ??????Option????????????????????У?????????Щ????????????????????????????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????????????Configuration????????β?λ??????????????????Option?????????????????????????????????????????????Option????????Option?????????????????????y?????????????????????????????????????????????????????Option????
????ASP.NET Core????????Option????OptionModel?????????????????????Option?????????????????????????????????????Option????????“Microsoft.Extensions.OptionModel”???NuGet???У??????????????????????????????????Option??????????????????????μ????????????????????????
????1: {
????2:   ...
????3:   "dependencies": {
????4:     "Microsoft.Extensions.OptionsModel"        : "1.0.0-rc1-final"??
????5:     "Microsoft.Extensions.DependencyInjection"    : "1.0.0-rc1-final"
????6:   }??
????7: }
??????????Option?????????????????????????????????????????????????FormatSettings??DateTimeFormatSettings??CurrencyDecimalSettings????????????????????????????????????????Main?????У???????????μ?????????????????????FormatSettings????
1: class Program
2: {
3:     static void Main(string[] args)
4:     {
5:         Dictionary<string?? string> source = new Dictionary<string?? string>
6:         {
7:             ["Format:DateTime:LongDatePattern"] = "dddd?? MMMM d?? yyyy"??
8:             ["Format:DateTime:LongTimePattern"] = "h:mm:ss tt"??
9:             ["Format:DateTime:ShortDatePattern"] = "M/d/yyyy"??
10:             ["Format:DateTime:ShortTimePattern"] = "h:mm tt"??
11:
12:             ["Format:CurrencyDecimal:Digits"] = "2"??
13:             ["Format:CurrencyDecimal:Symbol"] = "$"??
14:         };
15:         IConfiguration configuration = new ConfigurationBuilder()
16:                 .Add(new MemoryConfigurationProvider(source))
17:                 .Build()
18:                 .GetSection("Format"));
19:
20:         IOptions<FormatSettings> optionsAccessor = new ServiceCollection()
21:             .AddOptions()
22:             .Configure<FormatSettings>(configuration)
23:             .BuildServiceProvider()
24:             .GetService<IOptions<FormatSettings>>();
25:
26:         FormatSettings settings = optionsAccessor.Value;
27:
28:         Console.WriteLine("DateTime:");
29:         Console.WriteLine(" {0??-16}: {1}"?? "LongDatePattern"??settings.DateTime.LongDatePattern);
30:         Console.WriteLine(" {0??-16}: {1}"?? "LongTimePattern"??settings.DateTime.LongTimePattern);
31:         Console.WriteLine(" {0??-16}: {1}"?? "ShortDatePattern"??settings.DateTime.ShortDatePattern);
32:         Console.WriteLine(" {0??-16}: {1} "?? "ShortTimePattern"??settings.DateTime.ShortTimePattern);
33:
34:         Console.WriteLine("CurrencyDecimal:");
35:         Console.WriteLine(" {0??-16}: {1}"?? "Digits"??settings.CurrencyDecimal.Digits);
36:         Console.WriteLine(" {0??-16}: {1}"?? "Symbol"??settings.CurrencyDecimal.Symbol);
37:     }
38: }
????????????????????????????????ServiceCollection???????????????AddOptions????????Option????????????????????Configure??????FormatSettings???Option??????????Configuration??????????????????????ServiceCollection???????????ServiceProvider??????????GetService???????????????IOptions<FormatSettings>??????????Value?????????????????????FormatSettings????