| | 1 | | using System; |
| | 2 | | using System.ComponentModel; |
| | 3 | | using System.Reflection; |
| | 4 | |
|
| | 5 | | namespace PropertyGridHelpers.Attributes |
| | 6 | | { |
| | 7 | | #if NET8_0_OR_GREATER |
| | 8 | | /// <summary> |
| | 9 | | /// Specifies a user-friendly text representation for an Enum field, |
| | 10 | | /// primarily for display in a property grid or UI elements. |
| | 11 | | /// </summary> |
| | 12 | | /// <seealso cref="Attribute" /> |
| | 13 | | /// <param name="text">The display text for the Enum field.</param> |
| | 14 | | /// <remarks> |
| | 15 | | /// This attribute can be applied to individual Enum fields to provide a |
| | 16 | | /// custom display text. This is useful when showing Enum values in a |
| | 17 | | /// PropertyGrid, dropdowns, or UI components where a more descriptive |
| | 18 | | /// label is needed instead of the raw Enum name. |
| | 19 | | /// </remarks> |
| | 20 | | /// <example> |
| | 21 | | /// <code> |
| | 22 | | /// public enum Status |
| | 23 | | /// { |
| | 24 | | /// [EnumText("Pending Approval")] |
| | 25 | | /// Pending, |
| | 26 | | /// |
| | 27 | | /// [EnumText("Approved")] |
| | 28 | | /// Approved, |
| | 29 | | /// |
| | 30 | | /// [EnumText("Rejected")] |
| | 31 | | /// Rejected |
| | 32 | | /// } |
| | 33 | | /// </code> |
| | 34 | | /// </example> |
| | 35 | | [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)] |
| 124 | 36 | | public class EnumTextAttribute(string text) : Attribute |
| | 37 | | { |
| | 38 | | #else |
| | 39 | | /// <summary> |
| | 40 | | /// Specifies a user-friendly text representation for an Enum field, |
| | 41 | | /// primarily for display in a property grid or UI elements. |
| | 42 | | /// </summary> |
| | 43 | | /// <seealso cref="Attribute" /> |
| | 44 | | /// <remarks> |
| | 45 | | /// This attribute can be applied to individual Enum fields to provide a |
| | 46 | | /// custom display text. This is useful when showing Enum values in a |
| | 47 | | /// PropertyGrid, dropdowns, or UI components where a more descriptive |
| | 48 | | /// label is needed instead of the raw Enum name. |
| | 49 | | /// </remarks> |
| | 50 | | /// <example> |
| | 51 | | /// <code> |
| | 52 | | /// public enum Status |
| | 53 | | /// { |
| | 54 | | /// [EnumText("Pending Approval")] |
| | 55 | | /// Pending, |
| | 56 | | /// |
| | 57 | | /// [EnumText("Approved")] |
| | 58 | | /// Approved, |
| | 59 | | /// |
| | 60 | | /// [EnumText("Rejected")] |
| | 61 | | /// Rejected |
| | 62 | | /// } |
| | 63 | | /// </code> |
| | 64 | | /// </example> |
| | 65 | | [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)] |
| | 66 | | public class EnumTextAttribute : Attribute |
| | 67 | | { |
| | 68 | | /// <summary> |
| | 69 | | /// Initializes a new instance of the <see cref="EnumTextAttribute" /> class. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="text">The display text for the Enum field.</param> |
| 16 | 72 | | public EnumTextAttribute(string text) => EnumText = text; |
| | 73 | | #endif |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Gets the custom text associated with the enum field. |
| | 77 | | /// </summary> |
| | 78 | | /// <value> |
| | 79 | | /// A user-friendly string to display instead of the enum's raw name. |
| | 80 | | /// </value> |
| | 81 | | public string EnumText |
| | 82 | | { |
| 124 | 83 | | get; |
| | 84 | | #if NET8_0_OR_GREATER |
| 124 | 85 | | } = text; |
| | 86 | | #else |
| | 87 | | } |
| | 88 | | #endif |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Determines whether the specified enum value has an associated |
| | 92 | | /// <see cref="EnumTextAttribute"/>. |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="value">The enum value to check.</param> |
| | 95 | | /// <returns> |
| | 96 | | /// <c>true</c> if the attribute is applied to the enum field; otherwise, <c>false</c>. |
| | 97 | | /// </returns> |
| 28 | 98 | | public static bool Exists(Enum value) => Get(value) != null; |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Retrieves the <see cref="EnumTextAttribute"/> applied to the specified enum value. |
| | 102 | | /// </summary> |
| | 103 | | /// <param name="value">The enum value to retrieve the attribute from.</param> |
| | 104 | | /// <returns> |
| | 105 | | /// The <see cref="EnumTextAttribute"/> instance, or <c>null</c> if not found. |
| | 106 | | /// </returns> |
| | 107 | | public static EnumTextAttribute Get(Enum value) => |
| 236 | 108 | | value == null ? null |
| 236 | 109 | | : Support.Support.GetFirstCustomAttribute<EnumTextAttribute>( |
| 236 | 110 | | Support.Support.GetEnumField(value)); |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Gets the specified FieldInfo. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name="fi">The FieldInfo.</param> |
| | 116 | | /// <returns></returns> |
| | 117 | | public static EnumTextAttribute Get(FieldInfo fi) |
| 104 | 118 | | { |
| 112 | 119 | | if (fi == null || !fi.IsLiteral || !fi.IsStatic) |
| 20 | 120 | | return null; |
| | 121 | |
|
| | 122 | | try |
| 100 | 123 | | { |
| 108 | 124 | | var enumValue = (Enum)Enum.Parse(fi.FieldType, fi.Name); |
| 104 | 125 | | return Get(enumValue); |
| | 126 | | } |
| 20 | 127 | | catch |
| 12 | 128 | | { |
| 20 | 129 | | return null; |
| | 130 | | } |
| 112 | 131 | | } |
| | 132 | |
|
| | 133 | | /// <summary> |
| | 134 | | /// Retrieves the <see cref="EnumTextAttribute"/> using the supplied type descriptor context. |
| | 135 | | /// </summary> |
| | 136 | | /// <param name="context"> |
| | 137 | | /// The context providing information about the component and its property. |
| | 138 | | /// </param> |
| | 139 | | /// <returns> |
| | 140 | | /// The <see cref="EnumTextAttribute"/> if found, or <c>null</c> if not available. |
| | 141 | | /// </returns> |
| | 142 | | public static EnumTextAttribute Get(ITypeDescriptorContext context) => |
| 36 | 143 | | context == null |
| 36 | 144 | | ? null |
| 36 | 145 | | : context.Instance == null || context.PropertyDescriptor == null |
| 36 | 146 | | ? null |
| 36 | 147 | | : Get((Enum)context.PropertyDescriptor.GetValue(context.Instance)); |
| | 148 | | } |
| | 149 | | } |