要点

  • 支持将配置值绑定到已有对象
  • 支持将配置值绑定到私有属性上
    • 默认情况下我们只能绑定public

示例

继续沿用我们上一次创建的控制台应用程序,
新添加一个包Microsoft.Extensions.Configuration.Binder
image.png
首先我们新建一个类,来作为我们接受配置的实例

  1. class Config
  2. {
  3. public string Key1 { get; set; }
  4. public string Key2 { get; set; }
  5. public bool Key3 { get; set; }
  6. public int Key4 { get; set; }
  7. }

然后利用新添加的包,这个包的作用就是让我们能够很方便的把配置绑定到我们的强类型上面去。

  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.Primitives;
  3. using System;
  4. namespace ConfigurationFileDemo
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. var builder = new ConfigurationBuilder();
  11. var root = builder.AddJsonFile("appsettings.json").Build();
  12. var config = new Config
  13. {
  14. Key1 = "Config_Key1",
  15. Key2 = "Config_Key2",
  16. Key3 = true,
  17. Key4 = 10086
  18. };
  19. root.Bind(config);
  20. Console.WriteLine($"Key1 : {config.Key1}");
  21. Console.WriteLine($"Key2 : {config.Key2}");
  22. Console.WriteLine($"Key3 : {config.Key3}");
  23. Console.WriteLine($"Key4 : {config.Key4}");
  24. }
  25. }
  26. class Config
  27. {
  28. public string Key1 { get; set; }
  29. public string Key2 { get; set; }
  30. public bool Key3 { get; set; }
  31. public int Key4 { get; set; }
  32. }
  33. }

利用root.Bind(config);将其绑定到Config上。在此之前我们给这个config赋值。
执行代码:image.png 配置文件:image.png
可以看到,输出的值和我们之前定义的并不一样,说明这是从配置里读取出来的。

通常来说,我们的配置文件不会这么简单,一般都是嵌套的格式,我们将配置文件修改为以下形式

  1. {
  2. "Key1": "Value1",
  3. "Key2": "Value2",
  4. "Key3": false,
  5. "Key4": 10,
  6. "Other": {
  7. "Other_Key1": "Other_Value1",
  8. "Other_Key2": "Other_Value2",
  9. "Key4": 10010
  10. }
  11. }

在这种情形下面,我们就需要把我们的section绑定给我们的config对象

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var builder = new ConfigurationBuilder();
  6. var root = builder.AddJsonFile("appsettings.json").Build();
  7. var config = new Config
  8. {
  9. Key1 = "Config_Key1",
  10. Key2 = "Config_Key2",
  11. Key3 = true,
  12. Key4 = 10086
  13. };
  14. root.Bind(config);
  15. root.GetSection("Other").Bind(config);
  16. Console.WriteLine($"Key1 : {config.Key1}");
  17. Console.WriteLine($"Key2 : {config.Key2}");
  18. Console.WriteLine($"Key3 : {config.Key3}");
  19. Console.WriteLine($"Key4 : {config.Key4}");
  20. }
  21. }

我们在root.Bind(config)的下面,添加一行代码root.GetSection("Other").Bind(config),然后运行输出:
image.png
可以发现,从配置中读取值,然后第二行代码会读取到了Other的值,把之前的Key4的值也会覆盖。
一般情况下,我们会把配置的某些值设置为私有的,防止在别处修改。
Config类修改为如下

  1. class Config
  2. {
  3. public string Key1 { get; set; }
  4. public string Key2 { get; set; }
  5. public bool Key3 { get; set; }
  6. public int Key4 { get; private set; } = 10086;
  7. }

然后我们再去执行代码:
image.png
发现私有的属性并没有从配置中自动绑定上。
这时,我们需要利用Bind方法的参数,设置Options,将Bind方法修改为
root.Bind(config, configureOptions => { configureOptions.BindNonPublicProperties = true; });
BindNonPublicProperties默认是false
输出:image.png
然后把OtherBind方法也修改
root.GetSection("Other").Bind(config, configureOptions => { configureOptions.BindNonPublicProperties = true; });
输出:image.png