DynamicEditorControlFactory.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. private static bool GetControlType(Type editorType, Type valueType, [NotNullWhen(true)] out Type? TControl)
  43. {
  44. var controls = GetEditors().Where(x => editorType == x.Key || editorType.IsAssignableTo(x.Key));
  45. Type? editType = null;
  46. List<(Type valueType, Type editor)>? tControls = null;
  47. TControl = null;
  48. foreach(var control in controls)
  49. {
  50. if(control.Key == editorType)
  51. {
  52. tControls = control.Value;
  53. break;
  54. }
  55. if(TControl is null || editType!.IsAssignableTo(control.Key))
  56. {
  57. (editType, tControls) = control;
  58. }
  59. }
  60. if (tControls is null) return false;
  61. Type? _valueType = null;
  62. foreach(var control in tControls)
  63. {
  64. if(control.valueType == valueType)
  65. {
  66. TControl = control.editor;
  67. return true;
  68. }
  69. if(valueType.IsAssignableTo(control.valueType)
  70. && (_valueType is null || _valueType.IsAssignableTo(control.valueType)))
  71. {
  72. (_valueType, TControl) = control;
  73. }
  74. }
  75. return TControl is not null;
  76. }
  77. public static BaseDynamicEditorControl? CreateControl(BaseEditor editor, Type valueType, IDynamicEditorHost host)
  78. {
  79. if (GetControlType(editor.GetType(), valueType, out var TControl))
  80. {
  81. var result = Activator.CreateInstance(TControl) as BaseDynamicEditorControl;
  82. if (result != null)
  83. {
  84. result.ValueType = valueType;
  85. result.Host = host;
  86. result.EditorDefinition = editor;
  87. }
  88. return result;
  89. }
  90. return null;
  91. }
  92. }
  93. }