63 lines
1.7 KiB
C#
63 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using Decal.Adapter.Support;
|
|
using Decal.Interop.Inject;
|
|
|
|
namespace Decal.Adapter.Wrappers;
|
|
|
|
/// <summary>
|
|
/// Used for the registration and creation of control wrappers
|
|
/// </summary>
|
|
public static class ControlRegistry
|
|
{
|
|
private static Dictionary<Type, Type> myWrappers;
|
|
|
|
static ControlRegistry()
|
|
{
|
|
if (myWrappers == null)
|
|
{
|
|
myWrappers = new Dictionary<Type, Type>();
|
|
RegisterControls(Assembly.GetExecutingAssembly());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Scans an assembly and registers all of its control wrappers
|
|
/// </summary>
|
|
/// <param name="assembly">the assembly to scan</param>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a control wrapper for the passed in control
|
|
/// </summary>
|
|
/// <param name="control">the control to wrap</param>
|
|
/// <returns>the new wrapper</returns>
|
|
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;
|
|
}
|
|
}
|