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, List<(Type valueType, Type editor)>>? _editors;
- private static Dictionary<Type, List<(Type valueType, Type editor)>> GetEditors()
- {
- if(_editors is null)
- {
- _editors = [];
- foreach(var type in CoreUtils.TypeList(AppDomain.CurrentDomain.GetAssemblies(),
- x => x.IsClass
- && !x.IsAbstract
- && !x.IsGenericType
- && x.IsSubclassOfRawGeneric(typeof(DynamicEditorControl<,>))))
- {
- var editorClass = type.GetSuperclassDefinition(typeof(DynamicEditorControl<,>))!;
- var valueType = editorClass.GenericTypeArguments[0];
- var editor = editorClass.GenericTypeArguments[1];
- if (editor == typeof(IBaseEditor) || editor.HasInterface<IBaseEditor>())
- {
- _editors.GetValueOrAdd(editor).Add((valueType, type));
- }
- }
- }
- return _editors;
- }
- private static bool GetControlType(Type editorType, Type valueType, [NotNullWhen(true)] out Type? TControl)
- {
- var controls = GetEditors().Where(x => editorType == x.Key || editorType.IsAssignableTo(x.Key));
- Type? editType = null;
- List<(Type valueType, Type editor)>? tControls = null;
- TControl = null;
- foreach(var control in controls)
- {
- if(control.Key == editorType)
- {
- tControls = control.Value;
- break;
- }
- if(TControl is null || editType!.IsAssignableTo(control.Key))
- {
- (editType, tControls) = control;
- }
- }
- if (tControls is null) return false;
- Type? _valueType = null;
- foreach(var control in tControls)
- {
- if(control.valueType == valueType)
- {
- TControl = control.editor;
- return true;
- }
- if(valueType.IsAssignableTo(control.valueType)
- && (_valueType is null || _valueType.IsAssignableTo(control.valueType)))
- {
- (_valueType, TControl) = control;
- }
- }
- return TControl is not null;
- }
- public static BaseDynamicEditorControl? CreateControl(BaseEditor editor, Type valueType, IDynamicEditorHost host)
- {
- if (GetControlType(editor.GetType(), valueType, out var TControl))
- {
- var result = Activator.CreateInstance(TControl) as BaseDynamicEditorControl;
- if (result != null)
- {
- result.ValueType = valueType;
- result.Host = host;
- result.EditorDefinition = editor;
- }
- return result;
- }
- return null;
- }
-
- }
- }
|