簡體   English   中英

將 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中刪除

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);

應用設置.json:

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

有沒有一種簡單的方法可以將配置加載到對象中,或者我是否必須再次讀取配置文件並使用JsonConvert自己解析它?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM