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