< Summary - PropertyGridHelpers Code Coverage

Information
Class: PropertyGridHelpers.Converters.EnumTextConverter
Assembly: PropertyGridHelpers
File(s): File 1: C:\Agent\_work\2\s\Code\PropertyGridHelpers\Converters\EnumTextConverter.cs
File 2: C:\Agent\_work\2\s\Code\PropertyGridHelpers\Converters\EnumTextConverterT.cs
Tag: PropertyGridHelpers Build_2026.1.11.1_#580
Line coverage
100%
Covered lines: 64
Uncovered lines: 0
Coverable lines: 64
Total lines: 237
Line coverage: 100%
Branch coverage
100%
Covered branches: 44
Total branches: 44
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 3/4/2025 - 9:39:23 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: PropertyGridHelpers Build_2025.3.4.1_#3775/4/2025 - 8:18:24 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: PropertyGridHelpers Build_2025.5.4.1_#3905/22/2025 - 7:52:21 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: PropertyGridHelpers Build_2025.5.22.1_#4175/23/2025 - 7:21:57 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: Test PropertyGridHelpers Build_2025.5.23.2_#4265/28/2025 - 8:07:03 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: PropertyGridHelpers Build_2025.5.28.1_#4286/3/2025 - 4:27:27 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: Test PropertyGridHelpers Build_2025.6.3.1_#4336/3/2025 - 7:42:47 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: PropertyGridHelpers Build_2025.6.3.2_#4396/12/2025 - 9:38:24 PM Line coverage: 100% (70/70) Branch coverage: 100% (42/42) Total lines: 202 Tag: PropertyGridHelpers Build_2025.6.12.2_#4567/4/2025 - 11:01:20 PM Line coverage: 100% (57/57) Branch coverage: 100% (38/38) Total lines: 180 Tag: PropertyGridHelpers Build_2025.7.4.1_#4777/9/2025 - 2:20:26 PM Line coverage: 100% (59/59) Branch coverage: 100% (38/38) Total lines: 182 Tag: PropertyGridHelpers Build_2025.7.9.1_#4807/14/2025 - 8:32:11 PM Line coverage: 100% (63/63) Branch coverage: 100% (44/44) Total lines: 189 Tag: PropertyGridHelpers Build_2025.7.14.3_#4831/10/2026 - 4:46:27 PM Line coverage: 100% (64/64) Branch coverage: 100% (44/44) Total lines: 237 Tag: PropertyGridHelpers Build_2026.1.10.1_#579 3/4/2025 - 9:39:23 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: PropertyGridHelpers Build_2025.3.4.1_#3775/4/2025 - 8:18:24 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: PropertyGridHelpers Build_2025.5.4.1_#3905/22/2025 - 7:52:21 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: PropertyGridHelpers Build_2025.5.22.1_#4175/23/2025 - 7:21:57 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: Test PropertyGridHelpers Build_2025.5.23.2_#4265/28/2025 - 8:07:03 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: PropertyGridHelpers Build_2025.5.28.1_#4286/3/2025 - 4:27:27 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: Test PropertyGridHelpers Build_2025.6.3.1_#4336/3/2025 - 7:42:47 PM Line coverage: 100% (65/65) Branch coverage: 100% (40/40) Total lines: 195 Tag: PropertyGridHelpers Build_2025.6.3.2_#4396/12/2025 - 9:38:24 PM Line coverage: 100% (70/70) Branch coverage: 100% (42/42) Total lines: 202 Tag: PropertyGridHelpers Build_2025.6.12.2_#4567/4/2025 - 11:01:20 PM Line coverage: 100% (57/57) Branch coverage: 100% (38/38) Total lines: 180 Tag: PropertyGridHelpers Build_2025.7.4.1_#4777/9/2025 - 2:20:26 PM Line coverage: 100% (59/59) Branch coverage: 100% (38/38) Total lines: 182 Tag: PropertyGridHelpers Build_2025.7.9.1_#4807/14/2025 - 8:32:11 PM Line coverage: 100% (63/63) Branch coverage: 100% (44/44) Total lines: 189 Tag: PropertyGridHelpers Build_2025.7.14.3_#4831/10/2026 - 4:46:27 PM Line coverage: 100% (64/64) Branch coverage: 100% (44/44) Total lines: 237 Tag: PropertyGridHelpers Build_2026.1.10.1_#579

Metrics

MethodBlocks covered Blocks not covered Branch coverage Crap Score Cyclomatic complexity Line coverage
File 1: EnumTextConverter(...)20----
File 1: .ctor(...)--100%11100%
File 1: CanConvertTo(...)120----
File 1: CanConvertTo(...)--100%44100%
File 1: ConvertTo(...)310----
File 1: ConvertTo(...)--100%1212100%
File 1: ConvertTo(...)300----
File 1: CanConvertFrom(...)80----
File 1: CanConvertFrom(...)--100%22100%
File 1: ConvertFrom(...)530----
File 1: ConvertFrom(...)--100%2020100%
File 1: ConvertFrom(...)460----
File 1: GetStandardValues(...)150100%66100%
File 1: GetStandardValues(...)130----
File 2: EnumTextConverter()30----

File(s)

C:\Agent\_work\2\s\Code\PropertyGridHelpers\Converters\EnumTextConverter.cs

#LineLine coverage
 1using PropertyGridHelpers.Attributes;
 2using System;
 3using System.ComponentModel;
 4using System.Globalization;
 5using System.Linq;
 6using System.Reflection;
 7using static System.ComponentModel.TypeConverter;
 8
 9namespace 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>
 37647        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) =>
 3656            destinationType is null
 3657                ? throw new ArgumentNullException(nameof(destinationType))
 3658                : 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)
 16478        {
 18479            if (value == null) return null;
 16080            if (value.GetType() != EnumType)
 2081                throw new ArgumentException($"value is expected to be of type {EnumType}.", nameof(value));
 15682            if (destinationType == typeof(string))
 13683            {
 84                string results;
 14485                Attribute dna = LocalizedEnumTextAttribute.Get((Enum)value);
 14486                if (dna == null)
 11687                {
 12488                    dna = EnumTextAttribute.Get((Enum)value);
 12489                    results = dna == null
 12490                        ? value.ToString()
 12491                        : ((EnumTextAttribute)dna).EnumText;
 11692                }
 93                else
 3694                    results = ((LocalizedEnumTextAttribute)dna).GetLocalizedText(context, culture, EnumType);
 95
 14496                return results;
 97            }
 2898            else if (destinationType == typeof(int))
 2499                return (int)value;
 20100            return null;
 160101        }
 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) =>
 24108            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)
 60124        {
 76125            if (value == null) return null;
 60126            if (value is string stringValue)
 32127            {
 260128                foreach (var fi in EnumType.GetFields(BindingFlags.Public | BindingFlags.Static))
 100129                {
 130                    // 1. Check LocalizedEnumTextAttribute
 108131                    var localized = LocalizedEnumTextAttribute.Get(fi);
 108132                    if (localized != null)
 24133                    {
 32134                        var localizedText = localized.GetLocalizedText(context, culture, EnumType);
 32135                        if (string.Equals(stringValue, localizedText, StringComparison.Ordinal))
 20136                            return Enum.Parse(EnumType, fi.Name);
 20137                    }
 138
 139                    // 2. Check EnumTextAttribute
 104140                    var dna = EnumTextAttribute.Get(fi);
 141
 104142                    if ((dna != null) && (string.Equals((string)value, dna.EnumText, StringComparison.Ordinal)))
 24143                        return Enum.Parse(EnumType, fi.Name);
 88144                }
 145
 28146                if (Enum.GetNames(EnumType).Contains(value))
 20147                    return Enum.Parse(EnumType, (string)value);
 148                else
 16149                {
 24150                    var enums = Enum.GetValues(EnumType);
 24151                    return enums.GetValue(0);
 152                }
 153            }
 36154            else if (value is int intValue)
 20155            {
 28156                var s = Enum.GetName(EnumType, intValue);
 28157                var e = Enum.Parse(EnumType, s);
 28158                return e;
 159            }
 160
 24161            throw new ArgumentException("The value is expected to be a string or an int.", nameof(value));
 52162        }
 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)
 24175        {
 176            // Return all enum values for the property
 32177            if (context?.PropertyDescriptor != null)
 16178            {
 24179                var enumType = context.PropertyDescriptor.PropertyType;
 24180                if (enumType.IsEnum)
 20181                    return new StandardValuesCollection(Enum.GetValues(enumType));
 12182            }
 183
 28184            return base.GetStandardValues(context);
 24185        }
 186
 187        #endregion
 188    }
 189}

C:\Agent\_work\2\s\Code\PropertyGridHelpers\Converters\EnumTextConverterT.cs

#LineLine coverage
 1using PropertyGridHelpers.Attributes;
 2using System;
 3using System.ComponentModel;
 4using System.Windows.Forms;
 5
 6namespace PropertyGridHelpers.Converters
 7{
 8    /// <summary>
 9    /// A strongly-typed generic version of <see cref="EnumTextConverter"/> for handling enums with
 10    /// display text annotations in a <see cref="PropertyGrid"/>.
 11    /// </summary>
 12    /// <typeparam name="T">
 13    /// The enumeration type whose members are decorated with <see cref="EnumTextAttribute"/>.
 14    /// </typeparam>
 15    /// <remarks>
 16    /// This converter simplifies using <see cref="EnumTextAttribute"/> on enum members
 17    /// by automatically associating the generic parameter <typeparamref name="T"/> as
 18    /// the enum to convert. This enables the property grid to display friendly
 19    /// names for enum values without requiring manual type configuration.
 20    /// </remarks>
 21    /// <example>
 22    /// <code>
 23    /// public enum Status
 24    /// {
 25    ///     [EnumText("Pending Approval")]
 26    ///     Pending,
 27    ///
 28    ///     [EnumText("Approved")]
 29    ///     Approved,
 30    ///
 31    ///     [EnumText("Rejected")]
 32    ///     Rejected
 33    /// }
 34    ///
 35    /// [TypeConverter(typeof(EnumTextConverter&lt;Status&gt;))]
 36    /// public Status CurrentStatus { get; set; }
 37    /// </code>
 38    /// </example>
 39    /// <seealso cref="EnumTextAttribute"/>
 40    /// <seealso cref="EnumConverter"/>
 41    public partial class EnumTextConverter<T> : EnumTextConverter where T : Enum
 42    {
 43        /// <summary>
 44        /// Initializes a new instance of the <see cref="EnumTextConverter" /> class.
 45        /// </summary>
 1646        public EnumTextConverter() : base(typeof(T)) { }
 47    }
 48}