2
“其中T从xxx派生”我想用它的参数的泛型类和力量一个来自一些基类派生。 喜欢的东西:泛型:如何实现:
public class BaseClass { }
public class DerivedClass : BaseClass { }
public class Manager<T> where T : derivesfrom(BaseClass)
我现在做它的方式是在运行时在构造函数中:
public class Manager<T> where T : class
{
public Manager()
{
if (!typeof(T).IsSubclassOf(typeof(BaseClass)))
{
throw new Exception("Manager: Should not be here: The generic type should derive from BaseClass");
}
}
}
有没有办法在编译时做到这一点? 谢谢。
http://stackoverflow.com/questions/230736/can-c-sharp-generics-have-a-specific-base-type – Rawling