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...
使用泛型時等看來,編譯器將不會在 一般類型投下內通用類型(基本/子)(OtherBase/OtherSub)。爲什麼會發生?泛型鑄造
更新: 還請解釋一下上面的之間的區別如下(工作)
void Call<T>() where T : Base { }
//...
Call<Sub>();
這對於給出的示例有很大的意義,但仍然不適用於Call方法。你能否解釋一下你的例子和「void Call()其中T:Base {}」,「請致電()」的區別? –
jameszhao00
2011-06-11 15:28:22
@james真的,我的錯誤。修正了。您的擴展問題與通用差異完全無關,因此沒有理由不適用。 – 2011-06-11 15:34:20