Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement