< Summary - PropertyGridHelpers Code Coverage

Information
Class: PropertyGridHelpers.Attributes.LocalizedEnumTextAttribute
Assembly: PropertyGridHelpers
File(s): c:\agent\_work\9\s\Code\PropertyGridHelpers\Attributes\LocalizedEnumTextAttribute.cs
Tag: PropertyGridHelpers Build_2025.7.15.1_#485
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 130
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%
LocalizedEnumTextAttribute(...)20----
Get(...)60100%22100%
Get(...)170100%66100%
Get(...)150----
Get(...)120100%66100%

File(s)

c:\agent\_work\9\s\Code\PropertyGridHelpers\Attributes\LocalizedEnumTextAttribute.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 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)]
 4436    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>
 1678        public LocalizedEnumTextAttribute(string resourceKey) : base(resourceKey)
 879        {
 1680        }
 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) =>
 24892            value == null ? null
 24893                : Support.Support.GetFirstCustomAttribute<LocalizedEnumTextAttribute>(
 24894                    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)
 108102        {
 116103            if (fi == null || !fi.IsLiteral || !fi.IsStatic)
 20104                return null;
 105
 106            try
 104107            {
 112108                var enumValue = (Enum)Enum.Parse(fi.FieldType, fi.Name);
 108109                return Get(enumValue);
 110            }
 20111            catch
 12112            {
 20113                return null;
 114            }
 116115        }
 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) =>
 28126            context == null || context.Instance == null || context.PropertyDescriptor == null
 28127                ? null
 28128                : Get((Enum)context.PropertyDescriptor.GetValue(context.Instance));
 129    }
 130}