将IConfigurationSection绑定到没有ASP.NET Core的复杂对象 [英] Bind an IConfigurationSection to a complex object without ASP.NET Core
问题描述
我有一个.NET Core控制台应用程序,想要读取appsettings.json
并将一个节解析为List<Param>
(没有依赖关系注入或ASP.NET Core).
我已经尝试过如何我如何在.net Core应用程序中使用IConfiguration绑定多级配置对象?,但是似乎.Get<T>()
已从netcoreapp1.1
I have a .NET Core console application and want to read the appsettings.json
and parse a section as List<Param>
(without Dependency Injection or ASP.NET Core).
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:
appsettings.json:
{
"ListA": [
{ "ID": "123", "Param": "ABC"},
{ "ID": "123", "Param": "JKS"},
{ "ID": "456", "Param": "DEF"}
]
}
是否有一种简单的方法可以将配置加载到对象中,还是必须再次读取配置文件并用JsonConvert
自己解析?
Is there an 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
?
推荐答案
Get<T>
在程序包Microsoft.Extensions.Configuration.Binder
这篇关于将IConfigurationSection绑定到没有ASP.NET Core的复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!