【问题标题】:How to extra generic parameter after where statement?如何在 where 语句后添加通用参数?
【发布时间】:2009-11-05 03:39:26
【问题描述】:

我有一个抽象类如下:

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();        
  }    
}

那么,如果为名为 T2 的 Generic 添加 exta 参数,我该如何对此进行额外约束?

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

【问题讨论】:

    标签: oop .net-2.0 object


    【解决方案1】:
    abstract class BaseClass<T, **T2**> where T : BaseReturnType where T2 : BaseTypeForT2
    
        {        
          public abstract T PolymorphicMethod();    
        }
    

    根据here

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多