| | 1 | | using PropertyGridHelpers.Attributes; |
| | 2 | | using System; |
| | 3 | | using System.ComponentModel; |
| | 4 | | using System.Globalization; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Reflection; |
| | 7 | | using static System.ComponentModel.TypeConverter; |
| | 8 | |
|
| | 9 | | namespace PropertyGridHelpers.Converters |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Provides a type converter for enums that supports displaying custom user-friendly |
| | 13 | | /// text for enum fields using the <see cref="EnumTextAttribute"/> or |
| | 14 | | /// <see cref="LocalizedEnumTextAttribute"/>. |
| | 15 | | /// </summary> |
| | 16 | | /// <remarks> |
| | 17 | | /// This converter allows enum values to be shown in a property grid or dropdown list |
| | 18 | | /// with more readable labels instead of their default names. For localization or |
| | 19 | | /// specialized labeling, decorate the enum fields with <see cref="EnumTextAttribute"/> |
| | 20 | | /// or <see cref="LocalizedEnumTextAttribute"/>. |
| | 21 | | /// </remarks> |
| | 22 | | /// <example> |
| | 23 | | /// <code> |
| | 24 | | /// public enum Status |
| | 25 | | /// { |
| | 26 | | /// [EnumText("Pending Approval")] |
| | 27 | | /// Pending, |
| | 28 | | /// [EnumText("Approved")] |
| | 29 | | /// Approved, |
| | 30 | | /// [EnumText("Rejected")] |
| | 31 | | /// Rejected |
| | 32 | | /// } |
| | 33 | | /// |
| | 34 | | /// [TypeConverter(typeof(EnumTextConverter))] |
| | 35 | | /// public Status Status { get; set; } |
| | 36 | | /// </code> |
| | 37 | | /// </example> |
| | 38 | | /// <seealso cref="EnumConverter"/> |
| | 39 | | public partial class EnumTextConverter : EnumConverter |
| | 40 | | { |
| | 41 | | /// <summary> |
| | 42 | | /// Initializes a new instance of the <see cref="EnumTextConverter"/> class |
| | 43 | | /// for the given enum type. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="type">The target enum type to convert.</param> |
| | 46 | | /// <exception cref="ArgumentNullException">Thrown if <paramref name="type"/> is null.</exception> |
| 376 | 47 | | public EnumTextConverter(Type type) : base(type) { } |
| | 48 | |
|
| | 49 | | /// <inheritdoc/> |
| | 50 | | /// <remarks> |
| | 51 | | /// Supports conversion to <see cref="string"/> or <see cref="int"/> representations. |
| | 52 | | /// </remarks> |
| | 53 | | public override bool CanConvertTo( |
| | 54 | | ITypeDescriptorContext context, |
| | 55 | | Type destinationType) => |
| 36 | 56 | | destinationType is null |
| 36 | 57 | | ? throw new ArgumentNullException(nameof(destinationType)) |
| 36 | 58 | | : destinationType == typeof(string) || destinationType == typeof(int); |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Converts the specified enum value to a <see cref="string"/> or <see cref="int"/> |
| | 62 | | /// based on the requested destination type. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="context">An optional format context.</param> |
| | 65 | | /// <param name="culture">Culture information for the conversion.</param> |
| | 66 | | /// <param name="value">The value to convert, expected to be an enum of the specified type.</param> |
| | 67 | | /// <param name="destinationType">The target type for conversion (string or int).</param> |
| | 68 | | /// <returns>The converted value, or <c>null</c> if the input is <c>null</c>.</returns> |
| | 69 | | /// <exception cref="ArgumentException"> |
| | 70 | | /// Thrown if <paramref name="value"/> is not of the expected enum type, or |
| | 71 | | /// if the destination type is unsupported. |
| | 72 | | /// </exception> |
| | 73 | | public override object ConvertTo( |
| | 74 | | ITypeDescriptorContext context, |
| | 75 | | CultureInfo culture, |
| | 76 | | object value, |
| | 77 | | Type destinationType) |
| 164 | 78 | | { |
| 184 | 79 | | if (value == null) return null; |
| 160 | 80 | | if (value.GetType() != EnumType) |
| 20 | 81 | | throw new ArgumentException($"value is expected to be of type {EnumType}.", nameof(value)); |
| 156 | 82 | | if (destinationType == typeof(string)) |
| 136 | 83 | | { |
| | 84 | | string results; |
| 144 | 85 | | Attribute dna = LocalizedEnumTextAttribute.Get((Enum)value); |
| 144 | 86 | | if (dna == null) |
| 116 | 87 | | { |
| 124 | 88 | | dna = EnumTextAttribute.Get((Enum)value); |
| 124 | 89 | | results = dna == null |
| 124 | 90 | | ? value.ToString() |
| 124 | 91 | | : ((EnumTextAttribute)dna).EnumText; |
| 116 | 92 | | } |
| | 93 | | else |
| 36 | 94 | | results = ((LocalizedEnumTextAttribute)dna).GetLocalizedText(context, culture, EnumType); |
| | 95 | |
|
| 144 | 96 | | return results; |
| | 97 | | } |
| 28 | 98 | | else if (destinationType == typeof(int)) |
| 24 | 99 | | return (int)value; |
| 20 | 100 | | return null; |
| 160 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <inheritdoc/> |
| | 104 | | /// <remarks> |
| | 105 | | /// Supports conversion from <see cref="string"/> or <see cref="int"/> to the target enum. |
| | 106 | | /// </remarks> |
| | 107 | | public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => |
| 24 | 108 | | sourceType == typeof(string) || sourceType == typeof(int); |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Converts from a string or integer to the corresponding enum value. |
| | 112 | | /// </summary> |
| | 113 | | /// <param name="context">An optional format context.</param> |
| | 114 | | /// <param name="culture">Culture information for the conversion.</param> |
| | 115 | | /// <param name="value">The string or integer to convert.</param> |
| | 116 | | /// <returns>The parsed enum value.</returns> |
| | 117 | | /// <exception cref="ArgumentException"> |
| | 118 | | /// Thrown if <paramref name="value"/> is neither a string nor an int, or cannot be parsed. |
| | 119 | | /// </exception> |
| | 120 | | public override object ConvertFrom( |
| | 121 | | ITypeDescriptorContext context, |
| | 122 | | CultureInfo culture, |
| | 123 | | object value) |
| 60 | 124 | | { |
| 76 | 125 | | if (value == null) return null; |
| 60 | 126 | | if (value is string stringValue) |
| 32 | 127 | | { |
| 260 | 128 | | foreach (var fi in EnumType.GetFields(BindingFlags.Public | BindingFlags.Static)) |
| 100 | 129 | | { |
| | 130 | | // 1. Check LocalizedEnumTextAttribute |
| 108 | 131 | | var localized = LocalizedEnumTextAttribute.Get(fi); |
| 108 | 132 | | if (localized != null) |
| 24 | 133 | | { |
| 32 | 134 | | var localizedText = localized.GetLocalizedText(context, culture, EnumType); |
| 32 | 135 | | if (string.Equals(stringValue, localizedText, StringComparison.Ordinal)) |
| 20 | 136 | | return Enum.Parse(EnumType, fi.Name); |
| 20 | 137 | | } |
| | 138 | |
|
| | 139 | | // 2. Check EnumTextAttribute |
| 104 | 140 | | var dna = EnumTextAttribute.Get(fi); |
| | 141 | |
|
| 104 | 142 | | if ((dna != null) && (string.Equals((string)value, dna.EnumText, StringComparison.Ordinal))) |
| 24 | 143 | | return Enum.Parse(EnumType, fi.Name); |
| 88 | 144 | | } |
| | 145 | |
|
| 28 | 146 | | if (Enum.GetNames(EnumType).Contains(value)) |
| 20 | 147 | | return Enum.Parse(EnumType, (string)value); |
| | 148 | | else |
| 16 | 149 | | { |
| 24 | 150 | | var enums = Enum.GetValues(EnumType); |
| 24 | 151 | | return enums.GetValue(0); |
| | 152 | | } |
| | 153 | | } |
| 36 | 154 | | else if (value is int intValue) |
| 20 | 155 | | { |
| 28 | 156 | | var s = Enum.GetName(EnumType, intValue); |
| 28 | 157 | | var e = Enum.Parse(EnumType, s); |
| 28 | 158 | | return e; |
| | 159 | | } |
| | 160 | |
|
| 24 | 161 | | throw new ArgumentException("The value is expected to be a string or an int.", nameof(value)); |
| 52 | 162 | | } |
| | 163 | |
|
| | 164 | | #region GetStandardValues ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| | 165 | |
|
| | 166 | | /// <summary> |
| | 167 | | /// Returns a collection of standard enum values for the associated type. |
| | 168 | | /// </summary> |
| | 169 | | /// <param name="context">A type descriptor context providing information about the component.</param> |
| | 170 | | /// <returns> |
| | 171 | | /// A <see cref="StandardValuesCollection"/> containing the enum values, or the base |
| | 172 | | /// implementation if no enum type is detected. |
| | 173 | | /// </returns> |
| | 174 | | public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) |
| 24 | 175 | | { |
| | 176 | | // Return all enum values for the property |
| 32 | 177 | | if (context?.PropertyDescriptor != null) |
| 16 | 178 | | { |
| 24 | 179 | | var enumType = context.PropertyDescriptor.PropertyType; |
| 24 | 180 | | if (enumType.IsEnum) |
| 20 | 181 | | return new StandardValuesCollection(Enum.GetValues(enumType)); |
| 12 | 182 | | } |
| | 183 | |
|
| 28 | 184 | | return base.GetStandardValues(context); |
| 24 | 185 | | } |
| | 186 | |
|
| | 187 | | #endregion |
| | 188 | | } |
| | 189 | | } |