I've got the following structure of classes and I want my generic side to be able to access these two lists but I cannot get the conversion to work. I've tried various permutations of type guards and cast<> calls.
abstract class B {}
class X : B {}
class Y : B {}
class MyList<T> : List<T> where T : B {}
class Data
{
MyList<X> XList;
MyList<Y> YList;
MyList<T> GetList<T>() where T : B
{
if (typeof(T) == typeof(X))
return (MyList<T>)XList;
if (typeof(T) == typeof(Y))
return (MyList<T>)YList;
return new MyList<T>() { };
}
}
Pretty much the only constraint for all of this is that Data needs to be able to be loaded from a Blazor appsettings.json
somehow. Currently the structure looks like this:
{
"Configs": {
"XList": [
{
"Name": "X1",
// more data
},
{
"Name": "X2",
// more data
},
{
"Name": "X3",
// more data
}
],
"YList": [
{
"Name": "Y1",
// more data
},
{
"Name": "Y2",
// more data
}
]
},
}