从泛型基类派生的泛型类

时间:2014-02-20 22:27:00

标签: c# generics inheritance polymorphism abstract-class

假设以下课程

// What I have created ...

public abstract class TaxServiceProvider<T, S>
    where T : TaxServiceProviderConfig
    where S : TaxServiceInfo
{
    protected T Config { get; set; }

    public abstract S GetTax(int zipCode);
}

public abstract class TaxServiceInfo { ... }
public abstract class TaxServiceProviderConfig { ... }

// What I want to create ...

public class SpecialTaxServiceProvider<T, S> : TaxServiceProvider<SpecialTaxServiceProviderConfig, SpecialTaxServiceInfo>
    where T : SpecialTaxServiceProviderConfig
    where S : SpecialTaxServiceInfo
{ ... }
public class SpecialTaxServiceInfo : TaxServiceInfo { ... }
public class SpecialTaxServiceProviderConfig : TaxServiceProviderConfig { ... }

其中TaxServiceInfoTaxServiceProviderConfig用于支持TaxServiceProvider类。

我想从SpecialTaxServiceProvder创建一个派生类TaxServiceProvider(非抽象),它的通用方式与TaxServiceProvider相同,并且SpecialTaxServiceInfo和{ {1}}作为类型。

我想在SpecialTaxServiceProviderConfig中实施GetTaxConfig,以便SpecialTaxServiceProvider返回GetTax类型,SpecialTaxServiceInfo类型为{{1} }}

然后,我会创建一个派生自Config的附加类,以及派生自SpecialTaxServiceProviderConfigSpecialTaxServiceProvider

的类
SpecialTaxServiceInfo

此类的SpecialTaxServiceProviderConfig返回类型public class A_SpecialTaxServiceProvider : SpecialTaxServiceProvider<A_SpecialTaxServiceProviderConfig, A_SpecialTaxServiceInfo> { ... } public class A_SpecialTaxServiceProviderConfig : SpecialTaxServiceProviderConfig { ... } public class A_SpecialTaxServiceInfo : SpecialTaxServiceInfo { ... } ,此类的GetTax类型为A_SpecialTaxServiceInfo

我已经研究过C#中的协方差和泛型类的语法,但是我不确定我尝试做的是不可能的语言,或者我只是不知道设置它的正确方法起来。

2 个答案:

答案 0 :(得分:1)

  1. 更改SpecialTaxServiceProvider并将TS传递给TaxServiceProvider

    public class SpecialTaxServiceProvider<T, S> : TaxServiceProvider<T, S>
        where T : SpecialTaxServiceProviderConfig
        where S : SpecialTaxServiceInfo
    {
        public override S GetTax(int zipCode)
        {
            return null;
        }
    }
    
  2. 实施A_SpecialTaxServiceProvider并覆盖GetTax

    public class A_SpecialTaxServiceProvider : SpecialTaxServiceProvider<A_SpecialTaxServiceProviderConfig, A_SpecialTaxServiceInfo>
    {
        public override A_SpecialTaxServiceInfo GetTax(int zipCode)
        {
            return null;
        }
    }
    
  3. 它会阻止您创建

    public class A_SpecialTaxServiceProvider : SpecialTaxServiceProvider<A_SpecialTaxServiceProviderConfig, TaxServiceInfo>
    

    有以下错误:

      

    “Project.TaxServiceInfo”类型不能用作类型   泛型类型或方法中的参数'S'   'Project.SpecialTaxServiceProvider'。没有隐含的   引用从'Project.TaxServiceInfo'转换为   'Project.SpecialTaxServiceInfo'。

答案 1 :(得分:0)

我决定在构造函数中使用函数委托,允许我传入工厂方法来创建我的泛型类型。

这是我最终的结果

// Tier 1 of my class hierarchy
public abstract class TaxServiceProvider<C, I>
    where C : TaxServiceProviderConfig
    where I : TaxServiceInfo
{
    protected C Config { get; set; }

    public abstract I GetTax(int zipCode);
}

public abstract class TaxServiceInfo {
    public TaxServiceInfo(string param1, string param2, int param3, ect...) {
        ...
    }
}

public abstract class TaxServiceProviderConfig { ... }


// Tier 2 of my class hierarchy
public class DerivedTaxServiceProvider<C, I> : TaxServiceProvider<C, I>
    where C : DerivedTaxServiceProviderConfig
    where I : DerivedTaxServiceInfo
{
    protected Func<S, string, string, int, ect...> Factory;

    public DerivedTaxServiceProvider (C config, Func<I, string, string, int, ect...> factory) {
        Config = config;
        Factory = factory;
    }

    public override I GetTax(int zipCode) {
        ...
        I taxServiceInfo = Factory("param1", "param2", 3, ect...);
        ...
        return I;
    }
}

public class DerivedTaxServiceInfo : TaxServiceInfo {
    public DerivedTaxServiceInfo(string param1, string param2, int param3, ect...) 
        : base(param1, param2, param3, ect...) 
    { ... }
}

public class DerivedTaxServiceProviderConfig : TaxServiceProviderConfig { ... }

// Tier 3 of my class hierarchy
public class ConcreteTaxServiceProvider : DerivedTaxServiceProvider<ConcreteTaxServiceProviderConfig, ConcreteTaxServiceInfo> {
    public ConcreteTaxServiceProvider(ConcreteTaxServiceProviderConfig config, Func<ConcreteTaxServiceInfo, string, string, int, ect...> factory) {
        Config = config;
        Factory = factory;
    }

    public override ConcreteTaxServiceInfo GetTax(int zipCode) {
        return base.GetTax(zipCode);
    }
}

public class ConcreteTaxServiceInfo : DerivedTaxServiceInfo {
    public ConcreteTaxServiceInfo(string param1, string param2, int param3, ect...) 
        : base(param1, param2, param3, ect...) 
    { ... }

    public static ConcreteTaxServiceInfo CreateConcreteTaxServiceInfo(string param1, string param2, int param3, ect...) {
        return new ConcreteTaxServiceInfo(param1, param2, param3, etc...);
    }
}

public class ConcreteTaxServiceProviderConfig : DerivedTaxServiceProviderConfig { ... }

// Implementation of my class hierarchies
public void method() {
    ConcreteTaxServiceProviderConfig() config = new ConcreteTaxServiceProviderConfig();

    ConcreteTaxServiceProvider provider = new ConcreteTaxServiceProvider(config, ConcreteTaxServiceInfo.CreateConcreteTaxServiceInfo);

    ConcreteTaxServiceInfo serviceInfo = provider.GetTax(99939);
}

所以基本上我有2级泛型类。第二级Provider类重写了“GetTax”的基本抽象方法,但我不希望它返回一个具体类型,因为那时我无法在从它继承的类中干净地调用该方法。在具体(第3级)提供程序中调用“GetTax”时,我不得不将派生的ServiceInfo转换为ConcreteServiceInfo类型。

只要我在Info类中有一个与我的委托匹配的构造函数,无论Info类的子类化级别(在第二级以下),我都可以将它提供给我的提供者并使用第二级的Provider class GetTax方法。

这个工厂方法在Provider构造函数的参数列表中看起来很丑陋,而创建Info类的静态方法似乎很尴尬,但它确实有用!

这是我提出的提出这个解决方案的问题:C# generics problem - newing up the generic type with parameters in the constructor