Here is the setup.
public interface IBaseClass
{
}
public class DerivedClass1 : IBaseClass
{
}
public class DerivedClass2 : IBaseClass
{
}
public class WrapperClass<T> where T : IBaseClass
{
public T DClassItem {get;set;}
}
public List<WrapperClass<IBaseClass>> GetClasses()
{
List<WrapperClass<IBaseClass>> stuff = new List<WrapperClass<IBaseClass>>();
DerivedClass1 d1 = new DerivedClass1();
DerivedClass2 d2 = new DerivedClass2();
stuff.Add(GetWrapper(d1));
stuff.Add(GetWrapper(d2));
}
public GetWrapper<T>(T input) where T : IBaseClass
{
return new WrapperClass(){
DClassItem = input
};
}
At the end of GetClasses, I expect to have a list containing a WrapperClass<DerivedClass1>
and a WrapperClass<DerivedClass2>
. However, when I try to add the results from the GetWrapper call, it throws an error saying it can't convert from WrapperClass<DerivedClass1>
to WrapperClass<IBaseClass>
. Is there any way to get some kind of collection of all the instances of WrapperClass with its mix of different DerivedClass types?