interface Base { ... }
class Sub : Base { ... }
class OtherBase<T> where T : Base { ... }
class OtherSub<T> : OtherBase<T> where T : Base { ... }
//...in some class
void Call<T>() where T : OtherBase<Base> { }
//...
Call<OtherSub<Sub>>(); //compile fails...
似乎在使用泛型时,编译器不会将内部泛型类型(Base/Sub)转换为泛型类型(OtherBase/OtherSub)。为什么会发生这种情况?
更新:请解释一下上述内容和以下内容之间的区别(以下内容可以正常工作)。
void Call<T>() where T : Base { }
//...
Call<Sub>();