将IConfigurationSection绑定到没有aspnetcore的复杂对象

Max*_*Ast 7 c# configuration json .net-core .net-core-1.1

我有一个NetCore控制台应用程序,并希望读取appsettings.json和解析一个部分List<Param>(没有依赖注入或AspNetCore).
我已经尝试过如何在.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);
Run Code Online (Sandbox Code Playgroud)

appsettings.json:

{
  "ListA": [
    { "ID": "123", "Param": "ABC"},
    { "ID": "123", "Param": "JKS"},
    { "ID": "456", "Param": "DEF"}
  ]
}
Run Code Online (Sandbox Code Playgroud)

有没有一种简单的方法可以将配置加载到对象中,还是我必须再次读取配置文件并自己解析JsonConvert

Tre*_*iac 24

Get<T> 在包中定义 Microsoft.Extensions.Configuration.Binder

  • #loveHate是的,我一直在寻找并找到那个“额外的” nkpg。 (2认同)