我已经发布了我厂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
字符串。
我读过this所以我知道如何用反射来创建泛型方法。但实际上我不知道我应该在哪里创建它。在这样的工厂:
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;
}
}
你能说一下'Enum.TryParse'这行吗?我无法理解。另外,我不明白为什么你坚持重用泛型版本。掌握类型名称后,可以使用'CreateInstance'的另一个重载,它接受类型名称作为字符串。 – 2014-11-01 10:00:32
@WiktorZychla我有支持的控件的枚举,这很重要,因为我正在编写插件到Enterprise Architect,我不支持它提供的所有控件。所以我检查我的controlType是否支持类型。在第一篇文章中编辑版本。 – 2014-11-01 10:24:57