0
考慮這個泛型類:如何可以引用內泛型類型泛型類型在C#
class TList<T> : System.Collections.Generic.List<T> {
}
我還有一個泛型列表類包含這些名單,並可能需要對其成員的工作:
class TListList<U, T> : System.Collections.Generic.List<U> where U : TList<T>
{
public void Foo() {
foreach(U list in this) {
T bar = list[0];
}
}
}
下面是一個具體的實現:
class FooList : TList<Foo> {}
class FooListList : TListList<FooList, Foo> {}
我想什麼像要做的就是where
子句中的TListList
規範掉落T類參數,可以讓編譯器注意到它,並將其提供給TListList
成員:
class TListList<U> where U : TList<T> { ...same Foo() as above... }
class FooList : TList<Foo> {}
class FooListList : TListList<FooList> {}
這是可能的,我只是以錯誤的方式去做,或者是語言無法做到這一點?
這是不是一個真正的語言問題。注意你如何將'U'限制在'TList'。什麼是'T'?我們不知道,因爲'TListList '只依賴於'U',對'T'沒有任何說法。因此'T'不能被推斷出來,因爲它並不是作爲泛型類型參數公開的。 –
你應該放棄U而不是。有沒有進一步從TList <>進行推導? –