| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | using System;using System.Collections.Generic;using System.Diagnostics.CodeAnalysis;using System.Linq;using InABox.Core;using Syncfusion.Windows.Shared;namespace InABox.DynamicGrid{    public static class DynamicEditorControlFactory    {        private static Dictionary<Type, Type>? _editors;        public static void Register<TControl, TEditorBase, TEditor>()             where TEditor : TEditorBase            where TEditorBase : IBaseEditor            where TControl : BaseDynamicEditorControl<TEditorBase>, new()        {            _editors[typeof(TEditor)] = typeof(TControl);        }        public static void Register<TControl, TEditor>()            where TEditor : BaseEditor            where TControl : BaseDynamicEditorControl<TEditor>, new()            => Register<TControl, TEditor, TEditor>();        private static Dictionary<Type, Type> GetEditors()        {            if(_editors is null)            {                _editors = new();                foreach(var type in CoreUtils.TypeList(AppDomain.CurrentDomain.GetAssemblies(),                    x => x.IsClass                        && !x.IsAbstract                        && !x.IsGenericType                        && x.IsSubclassOfRawGeneric(typeof(BaseDynamicEditorControl<>))))                {                    var baseClass = type.GetSuperclassDefinition(typeof(BaseDynamicEditorControl<>))!;                    var editor = baseClass.GenericTypeArguments[0];                    if (editor == typeof(IBaseEditor) || editor.HasInterface<IBaseEditor>())                    {                        _editors[editor] = type;                    }                }            }            return _editors;        }        private static bool GetControlType(Type editorType, [NotNullWhen(true)] out Type? TControl)        {            var controls = GetEditors().Where(x => editorType == x.Key || editorType.IsAssignableTo(x.Key));            Type? editType = null;            TControl = null;            foreach(var control in controls)            {                if(control.Key == editorType)                {                    TControl = control.Value;                    return true;                }                if(TControl is null || editType!.IsAssignableTo(control.Key))                {                    (editType, TControl) = control;                }            }            return TControl is not null;        }        public static BaseDynamicEditorControl? CreateControl(BaseEditor editor, IDynamicEditorHost host)         {            if (GetControlType(editor.GetType(), out var TControl))            {                var result = Activator.CreateInstance(TControl) as BaseDynamicEditorControl;                if (result != null)                    result.Host = host;                return result;            }            return null;        }                public static BaseDynamicEditorControl? CreateControl<TEditor>(IDynamicEditorHost host) where TEditor : BaseEditor        {            if (GetControlType(typeof(TEditor), out var TControl))            {                var result = Activator.CreateInstance(TControl) as BaseDynamicEditorControl;                if (result != null)                    result.Host = host;                return result;            }            return null;        }            }}
 |