虽然应用程序可以直接利用通过IConfigurationBuilder对象创建的IConfiguration对象来提取配置数据,但是我们更倾向于将其转换成一个POCO对象,以面向对象的方式来使用配置,我们将这个转换过程称为配置绑定。配置绑定可以通过如下几个针对IConfiguration的扩展方法来实现,这些扩展方法都定义在NuGet包“Microsoft.Extensions.Configuration.Binder”中。
一、ConfigurationBinderpublic static class ConfigurationBinder { public static void Bind(this IConfiguration configuration, object instance); public static void Bind(this IConfiguration configuration, object instance, Action<BinderOptions> configureOptions); public static void Bind(this IConfiguration configuration, string key, object instance); public static T Get<T>(this IConfiguration configuration); public static T Get<T>(this IConfiguration configuration, Action<BinderOptions> configureOptions); public static object Get(this IConfiguration configuration, Type type); public static object Get(this IConfiguration configuration, Type type, Action<BinderOptions> configureOptions); } public class BinderOptions { public bool BindNonPublicProperties { get; set; } }