繁体   English   中英

反映工厂模式的通用方法

[英]Generic method with reflection in factory pattern

我已经将工厂发布到codereview.so,所以现在有了这个:

public class WpfControlFactory
{
    public static TControl CreateWpfControl<TControl>(string name = null) where TControl : class, IWpfControl
    {
        TControl wpfControl = default(TControl);

        //Avoid some bone-headed exceptions
        if (!typeof(TControl).IsAbstract)
        {
            wpfControl = Activator.CreateInstance<TControl>();
        }

        if (wpfControl != null)
        {
            wpfControl.Name = name ?? Consts.DefaultEaControlName;
        }

        return wpfControl;
    }
}

但是不幸的是我不能使用CreateWpfControl<TControl>()因为我没有TControl我只有typeName字符串。

我读过这篇文章,所以我知道如何使用反射创建泛型方法。 但是实际上我不知道应该在哪里创建它。 在这样的工厂中:

    public static IWpfControl CreateWpfControl(string controlType, string controlName)
    {
        Type type = FindType(controlType);
        if (type == null)
        {
            return null;
        }

        MethodInfo method = typeof(WpfControlFactory).GetMethod("CreateInstance");
        MethodInfo generic = method.MakeGenericMethod(type);
        return (IWpfControl)generic.Invoke(null, null);
    }

    private static Type FindType(string typeName)
    {
        Type type = null;
        WpfControl wpfControl;
        Enum.TryParse(typeName, out wpfControl);
        if (wpfControl != default(WpfControl))
        {
            type = Type.GetType(typeName);
        }

        return type;
    }

    private static TControl CreateInstance<TControl>(string name = null) where TControl : class, IWpfControl
    {
        TControl wpfControl = default(TControl);

        //Avoid some bone-headed exceptions
        if (!typeof(TControl).IsAbstract)
        {
            wpfControl = Activator.CreateInstance<TControl>();
        }

        if (wpfControl != null)
        {
            wpfControl.Name = name ?? Consts.DefaultEaControlName;
        }

        return wpfControl;
    }

还是在哪里? 我希望我的课程与SOLID保持一致

编辑

下一个可能的版本:

public class WpfControlFactory
{
    public static IWpfControl CreateWpfControl(string controlType, string controlName = null)
    {
        IWpfControl wpfControl = default(IWpfControl);

        Type type = Type.GetType(controlType);
        if (type != null && type.IsAssignableFrom(typeof(IWpfControl)))
        {
            wpfControl = (IWpfControl)Activator.CreateInstance(type);
        }

        if (wpfControl != null)
        {
            wpfControl.Name = controlName ?? Consts.DefaultEaControlName;
        }

        return wpfControl;
    }
}

第二种方法行不通,您无法从controlName获取类型。 我的想法是,如果您已经有一个封闭的枚举,请使用它。

public class WpfControlFactory
{
  public static IWpfControl CreateWpfControl(WpfControl control, string controlName)
  {
    IWpfControl wpfControl = default(IWpfControl);

    Type type = GetControl(control);
    if (type != null && type.IsAssignableFrom(typeof(IWpfControl)))
    {
        wpfControl = (IWpfControl)Activator.CreateInstance(type);
    }

    if (wpfControl != null)
    {
        wpfControl.Name = controlName ?? Consts.DefaultEaControlName;
    }

    return wpfControl;
  }

  private Type GetControl( WpfControl control )
  {
     switch ( control )
     {
        case WpfControl.Foo : return typeof( FooControlType );
        ...
     }
  }
}

这样,您就可以在枚举值和控件类型之间建立清晰的1-1映射。

另一方面,如果您依赖字符串,则查找类型要困难得多。 类型可以存在于不同的程序集中。 简短的类型名称可能还不够。 并且使用完整的类型名(还包括程序集名称)来使调用者更加困难(调用者必须知道类型才能获得其全名)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM