I'm exposing an API through a facade with methods returning interface types, and I have run into an issue with generic lists and inheritance in C#/.NET 3.5.
I have two interfaces:
IMyList (implements IList<IMyItem>)
IMyItem
And three classes:
MyList (implements IMyList, extends List<MyItem>)
MyOtherList (implements IMyList, extends ObservableCollection<MyItem>)
MyItem (implements IMyItem)
But it doesn't seem possible. How should I go about exposing only what is necessary, but still call the right implementations of the methods (that can vary for instance between MyList and MyOtherList)?
EDIT:
My facade is a factory looking something like this:
public static class Facade {
public static IMyList<IMyItem> CreateList() {
return new MyList<MyItem>();
}
public static IMyItem CreateItem() {
return new MyItem();
}
public static IConfiguration CreateConfiguration() {
return new Configuration();
}
}
Usage:
var list = Facade.CreateList();
list.DoSomethingOnChildren();
Now I'm expecting DoSomethingOnChildren() implemented in MyList to execute on a series of MyItem objects. If I was to change the method to return:
public static IMyList<IMyItem> CreateList() {
return new MyOtherList<MyOtherItem>();
}
I would expect DoSomethingOnChildren() implemented in MyOtherList to execute on a series of MyOtherItem objects.