PropertyResolver.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Media;
  7. using HandyControl.Properties.Langs;
  8. namespace HandyControl.Controls;
  9. public class PropertyResolver
  10. {
  11. private static readonly Dictionary<Type, EditorTypeCode> TypeCodeDic = new()
  12. {
  13. [typeof(string)] = EditorTypeCode.PlainText,
  14. [typeof(sbyte)] = EditorTypeCode.SByteNumber,
  15. [typeof(byte)] = EditorTypeCode.ByteNumber,
  16. [typeof(short)] = EditorTypeCode.Int16Number,
  17. [typeof(ushort)] = EditorTypeCode.UInt16Number,
  18. [typeof(int)] = EditorTypeCode.Int32Number,
  19. [typeof(uint)] = EditorTypeCode.UInt32Number,
  20. [typeof(long)] = EditorTypeCode.Int64Number,
  21. [typeof(ulong)] = EditorTypeCode.UInt64Number,
  22. [typeof(float)] = EditorTypeCode.SingleNumber,
  23. [typeof(double)] = EditorTypeCode.DoubleNumber,
  24. [typeof(bool)] = EditorTypeCode.Switch,
  25. [typeof(DateTime)] = EditorTypeCode.DateTime,
  26. [typeof(HorizontalAlignment)] = EditorTypeCode.HorizontalAlignment,
  27. [typeof(VerticalAlignment)] = EditorTypeCode.VerticalAlignment,
  28. [typeof(ImageSource)] = EditorTypeCode.ImageSource
  29. };
  30. public string ResolveCategory(PropertyDescriptor propertyDescriptor)
  31. {
  32. var categoryAttribute = propertyDescriptor.Attributes.OfType<CategoryAttribute>().FirstOrDefault();
  33. return categoryAttribute == null ?
  34. Lang.Miscellaneous :
  35. string.IsNullOrEmpty(categoryAttribute.Category) ?
  36. Lang.Miscellaneous :
  37. categoryAttribute.Category;
  38. }
  39. public string ResolveDisplayName(PropertyDescriptor propertyDescriptor)
  40. {
  41. var displayName = propertyDescriptor.DisplayName;
  42. if (string.IsNullOrEmpty(displayName))
  43. {
  44. displayName = propertyDescriptor.Name;
  45. }
  46. return displayName;
  47. }
  48. public string ResolveDescription(PropertyDescriptor propertyDescriptor) => propertyDescriptor.Description;
  49. public bool ResolveIsBrowsable(PropertyDescriptor propertyDescriptor) => propertyDescriptor.IsBrowsable;
  50. public bool ResolveIsDisplay(PropertyDescriptor propertyDescriptor) => propertyDescriptor.IsLocalizable;
  51. public bool ResolveIsReadOnly(PropertyDescriptor propertyDescriptor) => propertyDescriptor.IsReadOnly;
  52. public object ResolveDefaultValue(PropertyDescriptor propertyDescriptor)
  53. {
  54. var defaultValueAttribute = propertyDescriptor.Attributes.OfType<DefaultValueAttribute>().FirstOrDefault();
  55. return defaultValueAttribute?.Value;
  56. }
  57. public PropertyEditorBase ResolveEditor(PropertyDescriptor propertyDescriptor)
  58. {
  59. var editorAttribute = propertyDescriptor.Attributes.OfType<EditorAttribute>().FirstOrDefault();
  60. var editor = editorAttribute == null || string.IsNullOrEmpty(editorAttribute.EditorTypeName)
  61. ? CreateDefaultEditor(propertyDescriptor.PropertyType)
  62. : CreateEditor(Type.GetType(editorAttribute.EditorTypeName));
  63. return editor;
  64. }
  65. public virtual PropertyEditorBase CreateDefaultEditor(Type type) =>
  66. TypeCodeDic.TryGetValue(type, out var editorType)
  67. ? editorType switch
  68. {
  69. EditorTypeCode.PlainText => new PlainTextPropertyEditor(),
  70. EditorTypeCode.SByteNumber => new NumberPropertyEditor(sbyte.MinValue, sbyte.MaxValue),
  71. EditorTypeCode.ByteNumber => new NumberPropertyEditor(byte.MinValue, byte.MaxValue),
  72. EditorTypeCode.Int16Number => new NumberPropertyEditor(short.MinValue, short.MaxValue),
  73. EditorTypeCode.UInt16Number => new NumberPropertyEditor(ushort.MinValue, ushort.MaxValue),
  74. EditorTypeCode.Int32Number => new NumberPropertyEditor(int.MinValue, int.MaxValue),
  75. EditorTypeCode.UInt32Number => new NumberPropertyEditor(uint.MinValue, uint.MaxValue),
  76. EditorTypeCode.Int64Number => new NumberPropertyEditor(long.MinValue, long.MaxValue),
  77. EditorTypeCode.UInt64Number => new NumberPropertyEditor(ulong.MinValue, ulong.MaxValue),
  78. EditorTypeCode.SingleNumber => new NumberPropertyEditor(float.MinValue, float.MaxValue),
  79. EditorTypeCode.DoubleNumber => new NumberPropertyEditor(double.MinValue, double.MaxValue),
  80. EditorTypeCode.Switch => new SwitchPropertyEditor(),
  81. EditorTypeCode.DateTime => new DateTimePropertyEditor(),
  82. EditorTypeCode.HorizontalAlignment => new HorizontalAlignmentPropertyEditor(),
  83. EditorTypeCode.VerticalAlignment => new VerticalAlignmentPropertyEditor(),
  84. EditorTypeCode.ImageSource => new ImagePropertyEditor(),
  85. _ => new ReadOnlyTextPropertyEditor()
  86. }
  87. : type.IsSubclassOf(typeof(Enum))
  88. ? new EnumPropertyEditor()
  89. : new ReadOnlyTextPropertyEditor();
  90. public virtual PropertyEditorBase CreateEditor(Type type) => Activator.CreateInstance(type) as PropertyEditorBase ?? new ReadOnlyTextPropertyEditor();
  91. private enum EditorTypeCode
  92. {
  93. PlainText,
  94. SByteNumber,
  95. ByteNumber,
  96. Int16Number,
  97. UInt16Number,
  98. Int32Number,
  99. UInt32Number,
  100. Int64Number,
  101. UInt64Number,
  102. SingleNumber,
  103. DoubleNumber,
  104. Switch,
  105. DateTime,
  106. HorizontalAlignment,
  107. VerticalAlignment,
  108. ImageSource
  109. }
  110. }