DynamicEditorControlFactory.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, List<(Type valueType, Type editor)>>? _editors;
  12. private static Dictionary<Type, List<(Type valueType, Type editor)>> GetEditors()
  13. {
  14. if(_editors is null)
  15. {
  16. _editors = [];
  17. foreach(var type in CoreUtils.TypeList(AppDomain.CurrentDomain.GetAssemblies(),
  18. x => x.IsClass
  19. && !x.IsAbstract
  20. && !x.IsGenericType))
  21. {
  22. Type? valueType = null;
  23. Type? editor = null;
  24. if(type.GetSuperclassDefinition(typeof(DynamicEditorControl<,>)) is Type editorClass)
  25. {
  26. valueType = editorClass.GenericTypeArguments[0];
  27. editor = editorClass.GenericTypeArguments[1];
  28. }
  29. else if(type.GetSuperclassDefinition(typeof(DynamicEnclosedEditorControl<,>)) is Type enclosedEditorClass)
  30. {
  31. valueType = enclosedEditorClass.GenericTypeArguments[0];
  32. editor = enclosedEditorClass.GenericTypeArguments[1];
  33. }
  34. if (editor is not null && valueType is not null && (editor == typeof(IBaseEditor) || editor.HasInterface<IBaseEditor>()))
  35. {
  36. _editors.GetValueOrAdd(editor).Add((valueType, type));
  37. }
  38. }
  39. }
  40. return _editors;
  41. }
  42. /// <summary>
  43. /// Get the type for an editor control
  44. /// </summary>
  45. /// <param name="editorType">The type of <see cref="BaseEditor"/> the control needs to match.</param>
  46. /// <param name="valueType">The type of the property which the control will represent.</param>
  47. /// <param name="TControl">The <see cref="BaseDynamicEditorControl"/> that matches the arguments.</param>
  48. /// <returns><see langword="true"/> if a control was found.</returns>
  49. private static bool GetControlType(Type editorType, Type valueType, [NotNullWhen(true)] out Type? TControl)
  50. {
  51. // The controls dictionary is indexed by editor type, and in case of editor inheritance,
  52. // we need to get those that are equal and those simply assignable.
  53. var controls = GetEditors().Where(x => editorType == x.Key || editorType.IsAssignableTo(x.Key));
  54. Type? editType = null;
  55. List<(Type valueType, Type editor)>? tControls = null;
  56. // We need to find the best matching control type.
  57. TControl = null;
  58. foreach(var control in controls)
  59. {
  60. if(control.Key == editorType)
  61. {
  62. // If this editor control exactly matches the editor type.
  63. tControls = control.Value;
  64. break;
  65. }
  66. // Otherwise, if this control is a better pick than the current.
  67. if(editType is null || control.Key.IsAssignableTo(editType))
  68. {
  69. (editType, tControls) = control;
  70. }
  71. }
  72. if (tControls is null) return false;
  73. Type? _valueType = null;
  74. foreach(var control in tControls)
  75. {
  76. if(control.valueType == valueType)
  77. {
  78. TControl = control.editor;
  79. return true;
  80. }
  81. if(valueType.IsAssignableTo(control.valueType)
  82. && (_valueType is null || control.valueType.IsAssignableTo(_valueType)))
  83. {
  84. (_valueType, TControl) = control;
  85. }
  86. }
  87. return TControl is not null;
  88. }
  89. public static BaseDynamicEditorControl? CreateControl(BaseEditor editor, Type valueType, IDynamicEditorHost host)
  90. {
  91. if (GetControlType(editor.GetType(), valueType, out var TControl))
  92. {
  93. var result = Activator.CreateInstance(TControl) as BaseDynamicEditorControl;
  94. if (result != null)
  95. {
  96. result.ValueType = valueType;
  97. result.Host = host;
  98. result.EditorDefinition = editor;
  99. }
  100. return result;
  101. }
  102. return null;
  103. }
  104. }
  105. }