score:1

Accepted answer

Try something like this..

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

And testing...

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'
}

Edit: You don't actually need the template types for this

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

And your factory is simply:

class DietFactory : AbstractFactory<Diet>
{
    public DietFactory()
    {
    }
}

Related Articles