< Summary - PropertyGridHelpers Code Coverage

Information
Class: PropertyGridHelpers.Attributes.EnumImageAttribute
Assembly: PropertyGridHelpers
File(s): c:\agent\_work\9\s\Code\PropertyGridHelpers\Attributes\EnumImageAttribute.cs
Tag: PropertyGridHelpers Build_2025.7.15.1_#485
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 137
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
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
EnumImageAttribute()20----
.ctor()--100%11100%
EnumImageAttribute(...)20----
.ctor(...)--100%11100%
EnumImageAttribute(...)20----
.ctor(...)--100%11100%
get_EnumImage()--100%11100%
get_ImageLocation()--100%11100%
GetEnumImage(...)110100%44100%
GetEnumImage(...)100----
Exists(...)20100%11100%
Get(...)60100%22100%
Get(...)120100%66100%

File(s)

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

#LineLine coverage
 1using PropertyGridHelpers.Enums;
 2using PropertyGridHelpers.UIEditors;
 3using System;
 4using System.ComponentModel;
 5
 6namespace PropertyGridHelpers.Attributes
 7{
 8    /// <summary>
 9    /// Associates an image with an Enum field for display in a property grid.
 10    /// </summary>
 11    /// <remarks>
 12    /// This attribute allows an Enum field to have an associated image, which can be used
 13    /// in UI components such as property grids. The image is identified by a text-based key
 14    /// and can be stored as an embedded resource or in an external location.
 15    ///
 16    /// To display the image in a property grid, apply this attribute to the Enum items and
 17    /// use <see cref="ImageTextUIEditor"/> as the UI editor for the corresponding property.
 18    /// </remarks>
 19    /// <example>
 20    /// <code>
 21    /// public enum Status
 22    /// {
 23    ///     [EnumImage("PendingIcon.png")]
 24    ///     Pending,
 25    ///
 26    ///     [EnumImage("ApprovedIcon.png")]
 27    ///     Approved,
 28    ///
 29    ///     [EnumImage("RejectedIcon.png", ImageLocation.External)]
 30    ///     Rejected
 31    /// }
 32    /// </code>
 33    /// </example>
 34    /// <seealso cref="Attribute"/>
 35    /// <seealso cref="ImageTextUIEditor"/>
 36    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
 37    public class EnumImageAttribute : Attribute
 38    {
 39        /// <summary>
 40        /// Initializes a new instance of the <see cref="EnumImageAttribute"/> class
 41        /// with default values (null image, embedded location).
 42        /// </summary>
 2043        public EnumImageAttribute()
 1244        {
 2045            EnumImage = null;
 2046            ImageLocation = ImageLocation.Embedded;
 2047        }
 48
 49        /// <summary>
 50        /// Initializes a new instance of the <see cref="EnumImageAttribute"/> class
 51        /// with the specified image location.
 52        /// </summary>
 53        /// <param name="imageLocation">The storage location of the image (embedded or external).</param>
 2454        public EnumImageAttribute(ImageLocation imageLocation)
 1655        {
 2456            EnumImage = null;
 2457            ImageLocation = imageLocation;
 2458        }
 59
 60        /// <summary>
 61        /// Initializes a new instance of the <see cref="EnumImageAttribute"/> class
 62        /// with a specified image identifier and location.
 63        /// </summary>
 64        /// <param name="text">The name or resource key of the image.</param>
 65        /// <param name="imageLocation">The storage location of the image (default: embedded).</param>
 11266        public EnumImageAttribute(string text, ImageLocation imageLocation = ImageLocation.Embedded)
 10467        {
 11268            EnumImage = text;
 11269            ImageLocation = imageLocation;
 11270        }
 71
 72        /// <summary>
 73        /// Gets the name or resource key of the image associated with the Enum item.
 74        /// </summary>
 75        /// <value>
 76        /// The name or resource key of the image associated with the Enum item.
 77        /// </value>
 78        public string EnumImage
 79        {
 6080            get;
 81        }
 82
 83        /// <summary>
 84        /// Gets the storage location of the associated image.
 85        /// </summary>
 86        /// <value>
 87        /// The storage location of the associated image.
 88        /// </value>
 89        public ImageLocation ImageLocation
 90        {
 4491            get;
 92        }
 93
 94        /// <summary>
 95        /// Retrieves the name of the image associated with a given Enum value.
 96        /// </summary>
 97        /// <param name="value">The Enum value.</param>
 98        /// <returns>The image name if found, otherwise the Enum name.</returns>
 99        public static string GetEnumImage(Enum value)
 60100        {
 68101            var attribute = Get(value);
 68102            if (attribute == null)
 20103                return string.Empty;
 64104            var result = attribute.EnumImage;
 64105            if (string.IsNullOrEmpty(result))
 20106                result = Enum.GetName(value.GetType(), value);
 64107            return result;
 60108        }
 109
 110        /// <summary>
 111        /// Determines whether an <see cref="EnumImageAttribute"/> exists for the given Enum value.
 112        /// </summary>
 113        /// <param name="value">The Enum value.</param>
 114        /// <returns><c>true</c> if an image attribute is associated with the value; otherwise, <c>false</c>.</returns>
 24115        public static bool Exists(Enum value) => Get(value) != null;
 116
 117        /// <summary>
 118        /// Retrieves the <see cref="EnumImageAttribute"/> associated with a given Enum value.
 119        /// </summary>
 120        /// <param name="value">The Enum value.</param>
 121        /// <returns>The corresponding <see cref="EnumImageAttribute"/> instance, or <c>null</c> if none is found.</retu
 122        public static EnumImageAttribute Get(Enum value) =>
 136123            value == null ? null
 136124                : Support.Support.GetFirstCustomAttribute<EnumImageAttribute>(
 136125                    Support.Support.GetEnumField(value));
 126
 127        /// <summary>
 128        /// Gets the specified context.
 129        /// </summary>
 130        /// <param name="context">The context.</param>
 131        /// <returns></returns>
 132        public static EnumImageAttribute Get(ITypeDescriptorContext context) =>
 32133            context == null || context.Instance == null || context.PropertyDescriptor == null
 32134                ? null
 32135                : Get((Enum)context.PropertyDescriptor.GetValue(context.Instance));
 136    }
 137}