Это не сработает, потому что FooList<FooClass>
- это не IList<IFoo>
. Вот почему:
var myList = new FooList<FooClass>();
IFoo obj = new SomeOtherFooClass();
IList<IFoo> result = (IList<IFoo>)myList; // hypothetical, wouldn't actually work
result.Add(obj); // uh-oh, now myList has SomeOtherFooClass
Вам необходимо либо сделать копию, либо использовать интерфейс, который на самом деле является ковариантным для содержимого типа, например IEnumerable<T>
вместо IList<T>
. Или, если это уместно, вместо этого вы должны объявить FooList<FooClass>
как FooList<IFoo>
с самого начала.
Вот небольшая реализация, которая демонстрирует мое второе предложение:
public interface IFoo { }
public class FooClass : IFoo { }
public class FooList<T> : IList<T>
{
public void RemoveAt(int index) { /* ... */ }
/* further boring implementation of IList<T> goes here */
}
public static void ListConsumer(IList<IFoo> foos)
{
foos.RemoveAt(0); // or whatever
}
public static IList<IFoo> ListProducer()
{
// FooList<FooClass> foos = new FooList<FooClass>(); // would not work
FooList<IFoo> foos = new FooList<IFoo>();
foos.Add(new FooClass());
return foos; // a FooList<IFoo> is an IList<IFoo> so this is cool
}
public static void Demo()
{
ListConsumer(ListProducer()); // no problemo
}