| | 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 localized text representation for an enum field or property, |
| | 10 | | /// retrieving its display text from a resource file at runtime. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="resourceKey"> |
| | 13 | | /// The key identifying the localized text in the resource file. |
| | 14 | | /// </param> |
| | 15 | | /// <seealso cref="LocalizedTextAttribute"/> |
| | 16 | | /// <example> |
| | 17 | | /// <code> |
| | 18 | | /// public enum Status |
| | 19 | | /// { |
| | 20 | | /// [LocalizedEnumText("PendingApproval")] |
| | 21 | | /// Pending, |
| | 22 | | /// |
| | 23 | | /// [LocalizedEnumText("Approved")] |
| | 24 | | /// Approved, |
| | 25 | | /// |
| | 26 | | /// [LocalizedEnumText("Rejected")] |
| | 27 | | /// Rejected |
| | 28 | | /// } |
| | 29 | | /// </code> |
| | 30 | | /// </example> |
| | 31 | | /// <remarks> |
| | 32 | | /// Use this attribute to replace raw enum field names with user-friendly, |
| | 33 | | /// localized display text based on a resource key. |
| | 34 | | /// </remarks> |
| | 35 | | [AttributeUsage(AttributeTargets.Property | AttributeTargets.Enum | AttributeTargets.Field, AllowMultiple = false)] |
| 44 | 36 | | public class LocalizedEnumTextAttribute(string resourceKey) : LocalizedTextAttribute(resourceKey) |
| | 37 | | { |
| | 38 | | #else |
| | 39 | | /// <summary> |
| | 40 | | /// Specifies a localized text representation for an enum field or property, |
| | 41 | | /// retrieving its display text from a resource file at runtime. |
| | 42 | | /// </summary> |
| | 43 | | /// <seealso cref="LocalizedTextAttribute"/> |
| | 44 | | /// <remarks> |
| | 45 | | /// Initializes a new instance of the <see cref="LocalizedEnumTextAttribute"/> class |
| | 46 | | /// to link an enum field or property to a resource string key. |
| | 47 | | /// </remarks> |
| | 48 | | /// <example> |
| | 49 | | /// <code> |
| | 50 | | /// public enum Status |
| | 51 | | /// { |
| | 52 | | /// [LocalizedEnumText("PendingApproval")] |
| | 53 | | /// Pending, |
| | 54 | | /// |
| | 55 | | /// [LocalizedEnumText("Approved")] |
| | 56 | | /// Approved, |
| | 57 | | /// |
| | 58 | | /// [LocalizedEnumText("Rejected")] |
| | 59 | | /// Rejected |
| | 60 | | /// } |
| | 61 | | /// </code> |
| | 62 | | /// </example> |
| | 63 | | [AttributeUsage(AttributeTargets.Property | AttributeTargets.Enum | AttributeTargets.Field, AllowMultiple = false)] |
| | 64 | | public class LocalizedEnumTextAttribute : LocalizedTextAttribute |
| | 65 | | { |
| | 66 | | /// <summary> |
| | 67 | | /// Initializes a new instance of the <see cref="LocalizedEnumTextAttribute"/> class. |
| | 68 | | /// </summary> |
| | 69 | | /// <param name="resourceKey"> |
| | 70 | | /// The key identifying the localized text in the resource file. |
| | 71 | | /// </param> |
| | 72 | | /// <example> |
| | 73 | | /// <code> |
| | 74 | | /// [LocalizedEnumText("PropertyName_EnumText")] |
| | 75 | | /// public int PropertyName { get; set; } |
| | 76 | | /// </code> |
| | 77 | | /// </example> |
| 16 | 78 | | public LocalizedEnumTextAttribute(string resourceKey) : base(resourceKey) |
| 8 | 79 | | { |
| 16 | 80 | | } |
| | 81 | | #endif |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Retrieves the <see cref="LocalizedEnumTextAttribute"/> applied to the given |
| | 85 | | /// <paramref name="value"/>, if present. |
| | 86 | | /// </summary> |
| | 87 | | /// <param name="value">The enum value to look up.</param> |
| | 88 | | /// <returns> |
| | 89 | | /// The associated <see cref="LocalizedEnumTextAttribute"/>, or <c>null</c> if no attribute is applied. |
| | 90 | | /// </returns> |
| | 91 | | public static LocalizedEnumTextAttribute Get(Enum value) => |
| 248 | 92 | | value == null ? null |
| 248 | 93 | | : Support.Support.GetFirstCustomAttribute<LocalizedEnumTextAttribute>( |
| 248 | 94 | | Support.Support.GetEnumField(value)); |
| | 95 | |
|
| | 96 | | /// <summary> |
| | 97 | | /// Gets the specified FieldInfo. |
| | 98 | | /// </summary> |
| | 99 | | /// <param name="fi">The FieldInfo.</param> |
| | 100 | | /// <returns></returns> |
| | 101 | | public static LocalizedEnumTextAttribute Get(FieldInfo fi) |
| 108 | 102 | | { |
| 116 | 103 | | if (fi == null || !fi.IsLiteral || !fi.IsStatic) |
| 20 | 104 | | return null; |
| | 105 | |
|
| | 106 | | try |
| 104 | 107 | | { |
| 112 | 108 | | var enumValue = (Enum)Enum.Parse(fi.FieldType, fi.Name); |
| 108 | 109 | | return Get(enumValue); |
| | 110 | | } |
| 20 | 111 | | catch |
| 12 | 112 | | { |
| 20 | 113 | | return null; |
| | 114 | | } |
| 116 | 115 | | } |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Retrieves the <see cref="LocalizedEnumTextAttribute"/> from the given |
| | 119 | | /// <see cref="ITypeDescriptorContext"/>, if present. |
| | 120 | | /// </summary> |
| | 121 | | /// <param name="context">The type descriptor context.</param> |
| | 122 | | /// <returns> |
| | 123 | | /// The <see cref="LocalizedEnumTextAttribute"/>, or <c>null</c> if not found. |
| | 124 | | /// </returns> |
| | 125 | | public static new LocalizedEnumTextAttribute Get(ITypeDescriptorContext context) => |
| 28 | 126 | | context == null || context.Instance == null || context.PropertyDescriptor == null |
| 28 | 127 | | ? null |
| 28 | 128 | | : Get((Enum)context.PropertyDescriptor.GetValue(context.Instance)); |
| | 129 | | } |
| | 130 | | } |