C#, Generic Lists and Inheritance
- by Andy
I have a class called Foo that defines a list of objects of type A:
class Foo {
List<A> Items = new List<A>();
}
I have a class called Bar that can save and load lists of objects of type B:
class Bar {
void Save(List<B> ComplexItems);
List<B> Load();
}
B is a child of A. Foo, Bar, A and B are in a library and the user can create children of any of the classes.
What I would like to do is something like the following:
Foo MyFoo = new Foo();
Bar MyBar = new Bar();
MyFoo.Items = MyBar.Load();
MyBar.Save(MyFoo.Items);
Obviously this won't work. Is there a clever way to do this that avoids creating intermediate lists?
thanks, Andy