using System; using System.Collections.Generic; using System.Reflection; using Decal.Adapter.Support; using Decal.Interop.Inject; namespace Decal.Adapter.Wrappers; public static class ControlRegistry { private static Dictionary myWrappers; static ControlRegistry() { if (myWrappers == null) { myWrappers = new Dictionary(); RegisterControls(Assembly.GetExecutingAssembly()); } } public static void RegisterControls(Assembly assembly) { Type[] types = assembly.GetTypes(); foreach (Type type in types) { try { Type baseType = type.BaseType; if (!(baseType == null) && baseType.IsGenericType && baseType.GetGenericTypeDefinition() == typeof(ControlWrapperBase<>)) { Type key = baseType.GetGenericArguments()[0]; myWrappers.Add(key, type); } } catch (Exception ex) { Util.WriteLine("RegisterControls: " + ex.Message); } } } internal static IControlWrapper CreateInstance(IControl control) { Util.WriteLine("Creating wrapper for: " + control.GetType()); Type value; IControlWrapper controlWrapper = ((!myWrappers.TryGetValue(control.GetType(), out value)) ? new ControlWrapper() : ((IControlWrapper)Activator.CreateInstance(value))); controlWrapper.Initialize(control); return controlWrapper; } }