In C#, I am trying to store a private generic variable in a non-generic class that is passed in through a generic method. The generic method is supposed to take child classes that are upcast to the base class type.
Here is an example of the issue:
class BaseModel { }
class DerivedModel : BaseModel { }
class Data<T> where T : BaseModel { }
// Class is NOT generic
class DataConsumer
{
// How do I set this to generic?
private Data<BaseModel> data;
public void Set<T>(Data<T> data) where T : BaseModel
{
// Compile time error:
// Cannot convert source type 'Generic.Data<T>
// to target type 'Generic.Data<Generic.BaseModel>'
this.data = data;
}
}