< Summary - PropertyGridHelpers Code Coverage

Information
Class: PropertyGridHelpers.Attributes.EnumTextAttribute
Assembly: PropertyGridHelpers
File(s): c:\agent\_work\9\s\Code\PropertyGridHelpers\Attributes\EnumTextAttribute.cs
Tag: PropertyGridHelpers Build_2025.7.15.1_#485
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 149
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBlocks covered Blocks not covered Branch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)--100%11100%
EnumTextAttribute(...)20----
get_EnumText()--100%11100%
Exists(...)20100%11100%
Get(...)60100%22100%
Get(...)170100%66100%
Get(...)150----
Get(...)140100%66100%

File(s)

c:\agent\_work\9\s\Code\PropertyGridHelpers\Attributes\EnumTextAttribute.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel;
 3using System.Reflection;
 4
 5namespace 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)]
 12436    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>
 1672        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        {
 12483            get;
 84#if NET8_0_OR_GREATER
 12485        } = 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>
 2898        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) =>
 236108            value == null ? null
 236109                : Support.Support.GetFirstCustomAttribute<EnumTextAttribute>(
 236110                    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)
 104118        {
 112119            if (fi == null || !fi.IsLiteral || !fi.IsStatic)
 20120                return null;
 121
 122            try
 100123            {
 108124                var enumValue = (Enum)Enum.Parse(fi.FieldType, fi.Name);
 104125                return Get(enumValue);
 126            }
 20127            catch
 12128            {
 20129                return null;
 130            }
 112131        }
 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) =>
 36143            context == null
 36144                ? null
 36145                : context.Instance == null || context.PropertyDescriptor == null
 36146                    ? null
 36147                    : Get((Enum)context.PropertyDescriptor.GetValue(context.Instance));
 148    }
 149}