這是否可以用任何其他語法?帶泛型類型參數的網絡泛型類型
interface IFoo<T<U>> where U: Bar, new() where T<U>: class, IFoo<T<U>>, new()
這是一個辦法迫使通用論據IFoo的是一個通用型
class MyFoo<U>: IFoo<MyFoo<U>> where U: Bar, new()
編輯說明幾點:
- U是吧...這將成爲客戶實體
- T is IFoo ...它將成爲客戶業務對象
- IFoo is interfa行政長官對一些業務對象
- IFoo的定義
List<U> GetList(int compareToCustomerProperty)
編輯說明可用性:
interface IFoo<T<U>> where U: Bar, new() where T<U>: class, IFoo<T<U>>, new()
T<U> DoOnce();
class MyFoo<U>: IFoo<MyFoo<U>> where U: Bar, new()
MyFoo<U> DoOnce(){return this;}
MyFoo<U> DoAgain(){return this;}//this is not in interface
class Test{
public Test(){
IFoo<MyFoo<Bar>> x = new MyFoo<Bar>();//Generics can come as parameters
x.
DoOnce()//this is from interface
DoAgain();//Can access this x's method cause of generic in IFoo
}
}
我目前可以實現,但我失去了通用U.
可以沒有它? ?共
我會錯過這種方法在我的代碼中的某個點?羅!
我剛來到一個地步,我認爲這將是很好,在我的代碼(方法鏈抽象&混凝土之間,在仿製藥更多的自由)
感謝您的答案!
爲什麼'IFoo'怎麼在意'U'約束?什麼是用例? – 2012-03-07 02:44:27
Hi @ M.Babcock在編輯 – 2012-03-07 03:16:55
的第4點回答瞭解,但不應該'IFoo'將實例化'U'的責任委託給'T '? – 2012-03-07 03:21:17