I am building a console app using .NET 6. To load the settings I am using the Options Pattern and I want to create json config file with a collection of objects that has variations in one of its properties.
Example json:
{
"Persons": [
{
"Name": "Josh Wink",
"Information": {
"SSN": "BC00020032",
"VatId": "100099332"
},
"Characteristics": {
"Gender": "male",
"Age": 23
}
},
{
"Name": "Peter Gabriel",
"Information": {
"SSN": "GH00092921",
"VatId": "100003322"
},
"Characteristics": {
"EyeColor": "brown",
"HairColor": "black"
}
}
],
"SomeOtherSection": "Some Value"
}
I thought of using an empty interface for the Characteristics property, but I don't know how to map this section using Configuration.GetSection().Bind() or Configuration.GetSection().Get
Here are the classes I created
class PersonsCollection
{
List<Person> Persons { get; set;} = new();
}
class Person
{
string Name { get; set; } = String.Empty;
PersonInfo Information { get; set; } = new();
ICaracteristics? Characteristics { get; set; }
}
class PersonInfo
{
string SSN { get; set; } = String.Empty;
string VatId { get; set; } = String.Empty;
}
interface ICaracteristics
{
}
class PersonCharacteristicsType1 : ICaracteristics
{
string Name { get; set; } = String.Empty;
int Age { get; set; } = 0;
}
class PersonCharacteristicsType2 : ICaracteristics
{
string EyeColor { get; set; } = String.Empty;
string HairColor { get; set; } = String.Empty;
}