[英]Extracting custom object array from appsettings.json
I'm trying to bind appsettings file into a generic class, but currently I can't map List<object>
values.我正在尝试将 appsettings 文件绑定到通用 class,但目前我不能 map
List<object>
值。
I have an appsettings.Development.json hierarchy like this:我有一个 appsettings.Development.json 层次结构,如下所示:
{
"AppSettings":{
"ApplicationName":"FOO"
"MyValues":[
{
"Name":"Tryout1",
"QuestionCount": 7
},
{
"Name":"Tryout2"
"SettingName":"ABCDEFG"
}
]
}
}
And my generic class is like this:而我的通用 class 是这样的:
public class AppSettings
{
public string ApplicationName {get;set;}
public List<object> MyValues {get;set;}
}
When I use below code to bind this appsettings.Development.json into the generic class like this:当我使用下面的代码将此 appsettings.Development.json 绑定到通用 class 时,如下所示:
static ConfigurationHelper()
{
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.{environmentName}.json")
.Build();
try
{
var tryout = config.Get(typeof(AppSettings));
}
catch (Exception ex)
{
throw ex;
}
}
tryout
variable has the ApplicationName
filled with value foo
but the MyValues
is just an empty array. tryout
变量的ApplicationName
填充了值foo
,但MyValues
只是一个空数组。
I also tried using object[]
instead of List<object>
and at that point it recognizes the amount of objects but all of the elements have the value null
.我还尝试使用
object[]
而不是List<object>
,此时它可以识别对象的数量,但所有元素的值都为null
。
I don't want the MyValues
array to be pre-defined because every element can and will have different fields and I want to make this process as generic as possible.我不希望
MyValues
数组被预定义,因为每个元素都可以并且将有不同的字段,我想让这个过程尽可能通用。 How can I achieve this?我怎样才能做到这一点?
So, I found a way of achieving my goal, just not that straightforward but still pretty simple:所以,我找到了一种实现我的目标的方法,虽然不是那么简单但仍然非常简单:
First I had to change my appsettings.Development.json file like this:首先,我必须像这样更改我的 appsettings.Development.json 文件:
{
"AppSettings":{
"ApplicationName":"FOO"
"MyValues":[
"Tryout1": {
"Name":"Tryout1",
"QuestionCount": 7
},
"Tryout2": {
"Name":"Tryout2"
"SettingName":"ABCDEFG"
}
]
}
}
After this change, another change in the generic type is necessary, as follows:进行此更改后,还需要对泛型类型进行另一次更改,如下所示:
public class AppSettings
{
public string ApplicationName {get;set;}
public Dictionary<string, CustomBaseClass> MyValues {get;set;}
public List<object> _MyValues {get;set;}
}
public class CustomBaseClass
{
public string Name { get; set; }
}
public class Tryout1 : CustomBaseClass
{
public int QuestionCount {get;set;}
}
public class Tryout2 : CustomBaseClass
{
public string SettingName {get;set;}
}
This base class is important because we can't really bind the appsettings to an object, we still need a class. And base class, helps us bind these custom values like this:这个 base class 很重要,因为我们不能真正将 appsettings 绑定到 object,我们仍然需要一个 class。而 base class 可以帮助我们像这样绑定这些自定义值:
private static List<Type> CustomTypes = new List<Type> { typeof(Tryout1), typeof(Tryout2) };
static ConfigurationHelper()
{
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.{environmentName}.json")
.Build();
try
{
AppSettings = config.Get<AppSettingsContainer>();
foreach (var keyValuePair in AppSettings.MyValues)
{
var type = CustomTypes.Where(t => t.Name == keyValuePair.Key).FirstOrDefault();
if (type != null)
{
var myValue = config.GetSection(string.Format("AppSettings:MyValues:{0}", keyValuePair.Key)).Get(type);
if (job != null)
{
AppSettings._MyValues.Add(myValue);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
This should solve this problem, and huge thanks to andrewlock.net because I got the inital Dictionary<string, T>
idea from there.这应该可以解决这个问题,非常感谢andrewlock.net ,因为我从那里得到了最初的
Dictionary<string, T>
想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.