DynamicEditorControlFactory.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using InABox.Core;
  6. using Syncfusion.Windows.Shared;
  7. namespace InABox.DynamicGrid
  8. {
  9. public static class DynamicEditorControlFactory
  10. {
  11. private static Dictionary<Type, Type>? _editors;
  12. public static void Register<TControl, TEditorBase, TEditor>()
  13. where TEditor : TEditorBase
  14. where TEditorBase : IBaseEditor
  15. where TControl : BaseDynamicEditorControl<TEditorBase>, new()
  16. {
  17. _editors[typeof(TEditor)] = typeof(TControl);
  18. }
  19. public static void Register<TControl, TEditor>()
  20. where TEditor : BaseEditor
  21. where TControl : BaseDynamicEditorControl<TEditor>, new()
  22. => Register<TControl, TEditor, TEditor>();
  23. private static Dictionary<Type, Type> GetEditors()
  24. {
  25. if(_editors is null)
  26. {
  27. _editors = new();
  28. foreach(var type in CoreUtils.TypeList(AppDomain.CurrentDomain.GetAssemblies(),
  29. x => x.IsClass
  30. && !x.IsAbstract
  31. && !x.IsGenericType
  32. && x.IsSubclassOfRawGeneric(typeof(BaseDynamicEditorControl<>))))
  33. {
  34. var baseClass = type.GetSuperclassDefinition(typeof(BaseDynamicEditorControl<>))!;
  35. var editor = baseClass.GenericTypeArguments[0];
  36. if (editor == typeof(IBaseEditor) || editor.HasInterface<IBaseEditor>())
  37. {
  38. _editors[editor] = type;
  39. }
  40. }
  41. }
  42. return _editors;
  43. }
  44. private static bool GetControlType(Type editorType, [NotNullWhen(true)] out Type? TControl)
  45. {
  46. var controls = GetEditors().Where(x => editorType == x.Key || editorType.IsAssignableTo(x.Key));
  47. Type? editType = null;
  48. TControl = null;
  49. foreach(var control in controls)
  50. {
  51. if(control.Key == editorType)
  52. {
  53. TControl = control.Value;
  54. return true;
  55. }
  56. if(TControl is null || editType!.IsAssignableTo(control.Key))
  57. {
  58. (editType, TControl) = control;
  59. }
  60. }
  61. return TControl is not null;
  62. }
  63. public static BaseDynamicEditorControl? CreateControl(BaseEditor editor, IDynamicEditorHost host)
  64. {
  65. if (GetControlType(editor.GetType(), out var TControl))
  66. {
  67. var result = Activator.CreateInstance(TControl) as BaseDynamicEditorControl;
  68. if (result != null)
  69. result.Host = host;
  70. return result;
  71. }
  72. return null;
  73. }
  74. public static BaseDynamicEditorControl? CreateControl<TEditor>(IDynamicEditorHost host) where TEditor : BaseEditor
  75. {
  76. if (GetControlType(typeof(TEditor), out var TControl))
  77. {
  78. var result = Activator.CreateInstance(TControl) as BaseDynamicEditorControl;
  79. if (result != null)
  80. result.Host = host;
  81. return result;
  82. }
  83. return null;
  84. }
  85. }
  86. }