具有静态注册的工厂模式

edw*_*uck 3 c# factory static-constructor

我在尝试使用静态构造函数通过以下工厂注册我的类型时遇到问题:

 public class Factory<T>
{
    public static Factory<T> Instance { get { return _instance; } }

    private static Factory<T> _instance = new Factory<T>();
    private Factory() { }
    static Factory() { }

    static Dictionary<string, Type> _registeredType = new Dictionary<string, Type>();

    public void Register(string id, T obj)
    {
        if (obj.GetType().IsAbstract || obj.GetType().IsInterface)
            throw new ArgumentException("Cannot create instance of interface or abstract class");

        _registeredType.Add(id, obj.GetType());
    }

    public T Create(string id, params object[] parameters)
    {
        Type type;

        if(!_registeredType.TryGetValue(id, out type))
            throw new UnsupportedShapeException(id);

        return (T)Activator.CreateInstance(type, parameters);
    }
} 
Run Code Online (Sandbox Code Playgroud)

然后,如果我使用静态构造函数进行注册,它将不起作用:

    public interface IShape
{
    string print { get; }
}

public class Circle : IShape
{
    static Circle()
    {
        Factory<IShape>.Instance.Register("Circle", new Circle());
    }

    public string print
    {
        get
        {
            return "Circle";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我哪里错了?工厂看起来布置得很好,但我就是无法让医生工作。干杯。

Vad*_*nov 5

这不是一个答案,而是一个建议。首先,当您使用泛型类时,实际上 CLR 会为每个实现创建类。这些类将具有不同的静态变量,并且您不能对所有类使用一个工厂。好消息是您可以使用泛型方法而不是泛型类。你甚至不需要创建T对象的实例:

public class Factory
{
    public static Factory Instance { get { return _instance; } }

    private static Factory _instance = new Factory();
    private Factory() { }

    static Dictionary<string, Type> _registeredType = new Dictionary<string, Type>();

    public void Register<T>(string id)
    {
        var type = typeof(T);
        if (type.IsAbstract || type.IsInterface)
            throw new ArgumentException("Cannot create instance of interface or abstract class");

        _registeredType.Add(id, type);
    }

    public T Create<T>(string id, params object[] parameters)
    {
        Type type;

        if(!_registeredType.TryGetValue(id, out type))
            throw new UnsupportedShapeException(id);

        return (T) Activator.CreateInstance(type, parameters);
    }
} 
Run Code Online (Sandbox Code Playgroud)

现在您可以使用Factory来注册和解析对象:

Factory.Instance.Register<Circle>("Circle");
var firstCircle = Factory.Instance.Create<Circle>("Circle");
var secondCircle = Factory.Instance.Create<IShape>("Circle");
Run Code Online (Sandbox Code Playgroud)