使用反射在C#中实现自注册工厂

4
我希望获得有关以下工厂实现的反馈:

我想要一些有关以下工厂实现的反馈:

public enum DietType {Carnivore, Herbivore, Omnivore};

[AttributeUsage(System.AttributeTargets.Class)]
public class DietTypeAttribute : Attribute
{
    public DietType dietType { get; private set; }

    public DietTypeAttribute(DietType dietType)
    {
        this.dietType = dietType;
    }
}

public abstract class Diet { }

[DietTypeAttribute(DietType.Carnivore)]
public class Carnivore : Diet
{
}

[DietTypeAttribute(DietType.Herbivore)]
public class Herbivore : Diet
{
}

abstract class AbstractFactory<T> where T : class
{
    protected Dictionary<Enum, Type> types;

    protected AbstractFactory()
    {
    }

    public T CreateInstance(Enum id, params object[] param)
    {
        return (T)Activator.CreateInstance(types[id], param);
    }
}

class DietFactory : AbstractFactory<Diet>
{
    public DietFactory()
    {
        types = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                 from type in assembly.GetTypes()
                 let attributes = type.GetCustomAttributes(typeof(DietTypeAttribute), true)
                 where (attributes.Any()) && (typeof(Diet).IsAssignableFrom(type)) && (type.IsClass)
                 select
                 new
                 {
                     dietEnum = (Enum)((DietTypeAttribute)attributes.First()).dietType,
                     dietType = type
                 }).ToDictionary(x => x.dietEnum, x => x.dietType);
    }
}

使用方法:

static void Main(string[] args)
    {
        AbstractFactory<Diet> factory = new DietFactory();
        Diet diet = factory.CreateInstance(DietType.Carnivore);
    }

主要思想是使用枚举而不是字符串自我注册类。我正在努力找到一种方法使注册“通用化”,以便可以避免在LINQ查询中指定属性类。
欢迎任何帮助!

为什么要使用“枚举”?拥有一个具有“CreateInstance<TEntity>”的“工厂”不足以满足需求吗? - Taher Rahgooy
1个回答

1
尝试像这样做一些事情:

...

public enum DietType {Carnivore, Herbivore, Omnivore};

public class FactoryAttribute : Attribute
{
    public Object Something { get; protected set; }
}

[AttributeUsage(System.AttributeTargets.Class)]
public class DietTypeAttribute : FactoryAttribute
{
    public DietTypeAttribute(DietType dietType)
    {
        this.Something = dietType;
    }
}

public abstract class Diet { }

[DietTypeAttribute(DietType.Carnivore)]
public class Carnivore : Diet
{
}

[DietTypeAttribute(DietType.Herbivore)]
public class Herbivore : Diet
{
}

abstract class AbstractFactory<T> where T : class
{
    protected Dictionary<Enum, Type> types;

    protected AbstractFactory()
    {
    }

    protected void Register<TEnumType, TSubType>()
        where TEnumType: FactoryAttribute
    {

        types = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                 from type in assembly.GetTypes()
                 let attributes = type.GetCustomAttributes(typeof(TEnumType), true)
                 where (attributes.Any()) && (typeof(TSubType).IsAssignableFrom(type)) && (type.IsClass)
                 select
                 new
                 {
                     dietEnum = (Enum)((TEnumType)attributes.First()).Something,
                     dietType = type
                 }).ToDictionary(x => x.dietEnum, x => x.dietType);
    }

    public T CreateInstance(Enum id, params object[] param)
    {   
        return (T)Activator.CreateInstance(types[id], param);
    }
}

class DietFactory : AbstractFactory<Diet>
{
    public DietFactory()
    {
        Register<DietTypeAttribute, Diet>(); 
    }
}

并测试...

void Main()
{
    AbstractFactory<Diet> factory = new DietFactory();
    Diet diet = factory.CreateInstance(DietType.Carnivore);
    //diet is a 'Carnivore'
    diet = factory.CreateInstance(DietType.Herbivore);
    //diet is a 'Herbivore'
}

编辑:实际上你不需要模板类型来完成这个操作

abstract class AbstractFactory<T> where T : class
{
    protected Dictionary<Enum, Type> types;

    protected AbstractFactory()
    {
        types = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                 from type in assembly.GetTypes()
                 let attributes = type.GetCustomAttributes(typeof(FactoryAttribute), true)
                 where (attributes.Any()) && (typeof(T).IsAssignableFrom(type)) && (type.IsClass)
                 select
                 new
                 {
                     dietEnum = (Enum)((FactoryAttribute)attributes.First()).Something,
                     dietType = type
                 }).ToDictionary(x => x.dietEnum, x => x.dietType);
    }

    public T CreateInstance(Enum id, params object[] param)
    {   
        return (T)Activator.CreateInstance(types[id], param);
    }
}

"而你的工厂就是:"
class DietFactory : AbstractFactory<Diet>
{
    public DietFactory()
    {
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接