I'm trying to get the parameter from my appsettings.json which is a list of derived classes. The problem is that I don't know how I can retrieve the information and create the object with the good class for each item get.
My appsettings.json is something like this :
"DiagnosticSettings": {
"Diagnostics": [
{
"Type": "Connection",
"Address": "xxx"
},
{
"Type": "DNS",
"DNSAddress": "www.google.com",
"IPToCompare": "8.8.8.8"
},
{
"Type": "DNS",
"DNSAddress": "www.abc.com",
"IPToCompare": "x.x.x.x"
}, [...]
]}
I have a base class :
public class BaseDiagnosticConfig
{
public string Type { get; set; }
}
And derived classes like :
public class DNSDiagConfig : BaseDiagnosticConfig
{
public string DNSAddress { get; set; }
public string IPToCompare { get; set; }
}
I can retrieve the provider which contains the list of items with something like this :
var a = Configuration.GetSection("DiagnosticSettings:Diagnostics");
And I can have the list of base class (but it is not what I want as I lost all the derived classes information) :
var b = Configuration.GetSection("DiagnosticSettings:Diagnostics").Get<List<BaseDiagnosticConfig>>();
So there is a way to get my items which the good class even if I had to check one by one all the items in my appsettings list or I am just doing something impossible/wrong ?