I have the following classes:
public class BaseClass() {}
public class Class1 : BaseClass {}
public class Class2 : BaseClass {}
public class BaseClassList : List<BaseClass> {}
public class Class1List : List<Class1> {}
public class Class2List : List<Class2> {}
The data is in JSON format are is loaded as a list of BaseClass objects. Once loaded and want to populate the specific lists with the relevant objects:
public void Setup()
{
Class1List list1 = new Class1List();
var query = from x in BaseClassList
where x.Type = MyType.Class1
select x;
list1.AddRange(query);
}
However AddRange doesn't compile because I'm trying to added BaseClass objects to a Class1 list.
How can I successfully add the inherited classes?
BaseClassListis the name of a type - or do you have some property of that type too? It's also not clear why you have subclasses ofList<T>at all - that's usually not a great idea, IME.