在子类结果的抽象类中使用泛型方法

时间:2012-12-10 23:54:16

标签: c# generics

我正在努力干掉一些代码,我遇到了以下情况。我重新编写了代码,以提供更好的方案示例。

namespace SourceCode
{
    public interface IFactory
    {
        public baseClass GenerateClass();
        public bool IsUsable();
    } 

    public abstract baseClass
    {
        ...
    }

    public AClass:baseClass
    {
        ...
    }

    public class FactoryA:IFactory
    {
        public baseClass GenerateClass()
        {
            return new AClass();
        }

        public bool IsUsable(){
        {
            return true if some condition;
        }
    }

    public BClass:baseClass
    {
        ...
    }

    public class FactoryB:IFactory
    {
        public baseClass GenerateClass()
        {
            return new BClass();
        }

        public bool IsUsable(){
        {
            return true if some condition;
        }
    }

    public static class FactoryProvider
    {
        List<IFactory> factories
        static FactoryProvider()
        {
            factories.Add(new FactoryA());
            factories.Add(new FactoryB());
        }

        static List<baseClass> GetClasses()
        {
            return (from f in factories where f.IsUsable() select f).ToList();
        }
    }
}

namespace SourceCode.Tests
{
    public class baseTests
    {
        public T GenericMethod<T>(){...}
    }

    public class ClassATests:baseTests
    {
        public void Test1()
        {
            ... generic used in a method provided by the base class
        }
    }

    public class ClassBTests:baseTests
    {
        public void Test1()
        {
            ... generic used in a method provided by the base class
        }
    }
}

因此,我的测试中的问题是每个子类都必须进行测试。

UPDATE ======================= 我能够通过以下方式解决我的问题。

namespace SourceCode.Tests
{
    public class baseTests<I> where I: baseClass
    {
        public void Test1()
        {
            var result = GenericMethod<I>();
            // The generic method will use ClassA for ClassATests
            // and ClassB for ClassBTests
        }

        public T GenericMethod<T>(){...}
    }

    public class ClassATests:baseTests<ClassA>
    {            
    }

    public class ClassBTests:baseTests<ClassB>
    {            
    }
}

1 个答案:

答案 0 :(得分:1)

使您当前的A类通用可以解决问题:

abstract class A
{
    // protected members...
    protected abstract A InternalGetData(List<A> src);

    public void SharedMethodHappensAlways()
    {
        List<A> src = new List<A>();
        var childResult = InternalGetData(src);
    }  
}

abstract class A<T> : A
    where T: A
{   
    public T GetData(List<A> src)
    {
        return src.OfType<T>().FirstOrDefault();
    }

    protected override A InternalGetData(List<A> src)
    {
        return GetData(src);
    }
}    

class B : A<B>
{    
}

class C : A<C>
{    
}

样本用法:

List<A> src = new List<A>() { new B(), new C() };
B b = new B();
b.SharedMethodHappensAlways();
B firstB = b.GetData(src); // returns first instance of B
C firstC = new C().GetData(src);