Skip to main content
 首页 » 编程设计

c#之有约束为约束

2024年02月06日12yxwkf

我有这种情况:

public abstract class UblParser<TDto, TUbl> where TUbl : UblBaseDocumentType 
                                            where TDto: DtoB 
{ 
    public abstract TUbl ParseFrom(TDto dto); 
    public abstract TDto ParseTo(TUbl document); 
} 

我如何在这样的另一个约束中使用一个约束来声明一个类?

public class UblConverter<TParser> where TParser : UblParser<TDto, TUbl> 
                                   where TUbl : UblBaseDocumentType 
                                   where TDto : DtoB 
{ 
    ... 
} 

请您参考如下方法:

您必须在类定义中包含所有通用类型。

public class UblConverter<TParser, TDto, TUbl> where TParser : UblParser<TDto, TUbl>  
                                               where TUbl : UblBaseDocumentType  
                                               where TDto : DtoB  
{ 
    //... 
}