I'm trying to bind appsettings file into a generic class, but currently I can't map List<object>
values.
I have an appsettings.Development.json hierarchy like this:
{
"AppSettings":{
"ApplicationName":"FOO"
"MyValues":[
{
"Name":"Tryout1",
"QuestionCount": 7
},
{
"Name":"Tryout2"
"SettingName":"ABCDEFG"
}
]
}
}
And my generic class is like this:
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:
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.
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
.
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. How can I achieve this?