将 IConfigurationSection 绑定到没有 ASP.NET 内核的复杂对象



我有一个 .NET Core 控制台应用程序,想要读取appsettings.json并将部分解析为List<Param>(没有依赖注入或 ASP.NET Core(。
我已经尝试了如何在 .net Core 应用程序中使用 IConfiguration 绑定多级配置对象?但似乎.Get<T>()netcoreapp1.1中删除

IConfigurationSection myListConfigSection = configurationRoot.GetSection("ListA");
List<Param> paramList;
//Get does not exist
//paramList = myListConfigSection.Get<Param>();
string paramListJson = myListConfigSection.Value // is null
// won't work neither because paramListJson is null
paramList = JsonConvert.DeserializeObject<Param>(paramListJson);

appsettings.json:

{
"ListA": [
{ "ID": "123", "Param": "ABC"},
{ "ID": "123", "Param": "JKS"},
{ "ID": "456", "Param": "DEF"}
]
}

有没有一种简单的方法将配置加载到对象中,或者我是否必须再次读取配置文件并使用JsonConvert自己解析它?

Get<T>

在包Microsoft.Extensions.Configuration.Binder中定义

最新更新