这是否可以用任何其他语法?带泛型类型参数的网络泛型类型
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