如何在where语句后添加泛型参数?

时间:2009-11-05 03:39:27

标签: oop .net-2.0 object

我有一个抽象类如下:

class BaseReturnType { }    
class DerivedReturnType : BaseReturnType { }    

abstract class BaseClass<T> where T : BaseReturnType    
{        
  public abstract T PolymorphicMethod();    
}    

class DerivedClass : BaseClass<DerivedReturnType>    
{
  public override DerivedReturnType PolymorphicMethod()        
  {            
    return new DerivedReturnType();        
  }    
}

因此,如果为Generic添加额外参数T2,我该如何对此进行extrac约束?

abstract class BaseClass<T, **T2**> where T : BaseReturnType ???  
    {        
      public abstract T PolymorphicMethod();    
    }   

1 个答案:

答案 0 :(得分:4)

abstract class BaseClass<T, **T2**> where T : BaseReturnType where T2 : BaseTypeForT2

    {        
      public abstract T PolymorphicMethod();    
    }

根据here