我认为这是以下问题的一个边缘案例:
Cast List<T> to List<Interface>
我同意在这种情况下这看起来很奇怪,因为它不可能从扩展string的东西(string类是密封的)进行转换,但是我猜规则是不能转换的
List<T>
到
List<SomethingElse>
因为在T是扩展类SomethingElse或实现名为SomethingElse的接口的类的情况下,可能会调用该方法。
例如。:
public static void Foo(List<string> strings) { }
public static void Bar<T>(List<T> ts)
{
Foo((List<MyInterface>)ts);
}
public static void OtherMethod1()
{
Bar<MyClass>(new List<MyClass>)
}
public static void OtherMethod2()
{
Bar<MyInterface>(new List<MyInterface>)
}
public classs MyClass : MyInterface
{
}
因此,因为有可能编写一个像OtherMethod1这样在运行时失败的方法,编译器不允许整个场景。