相关疑难解决方法(0)

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

我有一个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

c# configuration json .net-core .net-core-1.1

7
推荐指数
1
解决办法
6705
查看次数

标签 统计

.net-core ×1

.net-core-1.1 ×1

c# ×1

configuration ×1

json ×1