我正在努力解决如何做到这一点: 我试图让 DoSomething 方法接受从 GenericClass < T > 派生的所有类,而不必将 GenericDerived 类分解为 < GenericDerived, Base >。
class Program
{
static void Main(string[] args)
{
SomeClass.DoSomething<GenericDerived>(new GenericDerived()); //Still not compiler friendly
}
}
class Base { }
class GenericClass<T> where T : Base { }
class GenericDerived : GenericClass<Base> { }
class SomeClass
{
public static void DoSomething<M, B>(M instance)
where B : Base
where M : GenericClass<B>
{
// Do something...
}
}
请您参考如下方法:
您的示例中没有名为 T 的类型参数。您还需要添加它及其约束。
class SomeClass
{
public void DoSomething<M,T>(M instance) where T : Base
where M : GenericClass<T>
{
// Do something...
}
}
