0

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?

Jonathan
  • 109
  • 1
  • 3
  • Servy beat me to the dupe, but wanted to also say that your code has multiple errors that you should have fixed before posting here. – DavidG Jun 12 '18 at 17:49
  • Can I request changing the duplicate link to this answer instead? https://stackoverflow.com/questions/3215402/collection-of-generic-types The answer linked wasn't helpful in resolving the issue. This answer is more like what I'm trying to do, and put me on the right track to an answer. Thanks. – Jonathan Jun 12 '18 at 20:10

0 Answers0