Bind a IConfigurationSection to an complex object without aspnetcore

*爱你&永不变心* 提交于 2020-01-03 08:36:50

问题


I have a NetCore console application and want to read the appsettings.json and parse a section as List<Param> (without Dependency Injection or AspNetCore).
I already tried How do I bind a multi level configuration object using IConfiguration in a .net Core application? but it seems like .Get<T>() got removed from 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"}
  ]
}

Is there a easy way to load the config into the object or do I have to read the config file again and parse it myself with JsonConvert?


回答1:


Get<T> is defined in package Microsoft.Extensions.Configuration.Binder



来源:https://stackoverflow.com/questions/45039136/bind-a-iconfigurationsection-to-an-complex-object-without-aspnetcore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!