可能重复:
Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates?
In C# 4.0, is it possible to derive a class from a generic type parameter?泛型类继承其通用型
我试图创建一个从它的泛型类型继承的泛型类(1。尝试)。看起来不可能。但在其他尝试中,我必须抛出物体。任何解决这个问题的设计?
// #1 Attempt
public interface IFoo { }
public class Generic<T> : T { }
public void play()
{
IFoo genfoo = new Generic<IFoo>();
}
。
// #2 Attempt. Castable solution
public interface IAnything { }
public interface IFoo2 : IAnything { }
public class Generic2<T> : IAnything { }
public void play2()
{
IFoo2 genFoo = (IFoo2) new Generic2<IFoo2>();
}
我看不出这是可能的,有或没有铸造。你想做什么?根据你的情况,它可能是你正在寻找类似Castle.DynamicProxy的东西,或者是一个嘲讽的框架...... – MattDavey 2011-05-05 09:27:38
有一个泛型类必须从泛型类型继承,这有什么意义?它**违背了通用目标。 – Aliostad 2011-05-05 09:28:24
你是什么意思,*继承*或*实现*?你说“继承”,但给一个接口作为例子。 – 2011-05-05 09:32:18