Getting attributes of Enum's value

后端 未结 25 2577
慢半拍i
慢半拍i 2020-11-22 00:35

I would like to know if it is possible to get attributes of the enum values and not of the enum itself? For example, suppose I have the following <

相关标签:
25条回答
  • 2020-11-22 01:25

    I've merged a couple of the answers here to create a little more extensible solution. I'm providing it just in case it's helpful to anyone else in the future. Original posting here.

    using System;
    using System.ComponentModel;
    
    public static class EnumExtensions {
    
        // This extension method is broken out so you can use a similar pattern with 
        // other MetaData elements in the future. This is your base method for each.
        public static T GetAttribute<T>(this Enum value) where T : Attribute {
            var type = value.GetType();
            var memberInfo = type.GetMember(value.ToString());
            var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
            return attributes.Length > 0 
              ? (T)attributes[0]
              : null;
        }
    
        // This method creates a specific call to the above method, requesting the
        // Description MetaData attribute.
        public static string ToName(this Enum value) {
            var attribute = value.GetAttribute<DescriptionAttribute>();
            return attribute == null ? value.ToString() : attribute.Description;
        }
    
    }
    

    This solution creates a pair of extension methods on Enum. The first allows you to use reflection to retrieve any attribute associated with your value. The second specifically calls retrieves the DescriptionAttribute and returns it's Description value.

    As an example, consider using the DescriptionAttribute attribute from System.ComponentModel

    using System.ComponentModel;
    
    public enum Days {
        [Description("Sunday")]
        Sun,
        [Description("Monday")]
        Mon,
        [Description("Tuesday")]
        Tue,
        [Description("Wednesday")]
        Wed,
        [Description("Thursday")]
        Thu,
        [Description("Friday")]
        Fri,
        [Description("Saturday")]
        Sat
    }
    

    To use the above extension method, you would now simply call the following:

    Console.WriteLine(Days.Mon.ToName());
    

    or

    var day = Days.Mon;
    Console.WriteLine(day.ToName());
    
    0 讨论(0)
  • 2020-11-22 01:25

    Performance matters

    If you want better performance this is the way to go:

    public static class AdvancedEnumExtensions
    {
        /// <summary>
        /// Gets the custom attribute <typeparamref name="T"/> for the enum constant, if such a constant is defined and has such an attribute; otherwise null.
        /// </summary>
        public static T GetCustomAttribute<T>(this Enum value) where T : Attribute
        {
            return GetField(value)?.GetCustomAttribute<T>(inherit: false);
        }
    
        /// <summary>
        /// Gets the FieldInfo for the enum constant, if such a constant is defined; otherwise null.
        /// </summary>
        public static FieldInfo GetField(this Enum value)
        {
            ulong u64 = ToUInt64(value);
            return value
                .GetType()
                .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
                .Where(f => ToUInt64(f.GetRawConstantValue()) == u64)
                .FirstOrDefault();
        }
    
        /// <summary>
        /// Checks if an enum constant is defined for this enum value
        /// </summary>
        public static bool IsDefined(this Enum value)
        {
            return GetField(value) != null;
        }
    
        /// <summary>
        /// Converts the enum value to UInt64
        /// </summary>
        public static ulong ToUInt64(this Enum value) => ToUInt64((object)value);
    
        private static ulong ToUInt64(object value)
        {
            switch (Convert.GetTypeCode(value))
            {
                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                    return unchecked((ulong)Convert.ToInt64(value, CultureInfo.InvariantCulture));
    
                case TypeCode.Byte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                case TypeCode.Char:
                case TypeCode.Boolean:
                    return Convert.ToUInt64(value, CultureInfo.InvariantCulture);
    
                default: throw new InvalidOperationException("UnknownEnumType");
            }
        }
    }
    

    Why does this have better performance?

    Because the built-in methods all use code very similar to this except they also run a bunch of other code we don't care about. C#'s Enum code is quite horrible in general.

    The above code has been Linq-ified and streamlined so it only contains the bits we care about.

    Why is the built-in code slow?

    First regarding Enum.ToString() -vs- Enum.GetName(..)

    Always use the latter. (Or better yet neither, as will become clear below.)

    ToString() uses the latter internally, but again, also does a bunch of other stuff we don't want, e.g. tries to combine flags, print out numbers etc. We are only interested in constants defined inside the enum.

    Enum.GetName in turn gets all fields, creates a string array for all names, uses the above ToUInt64 on all of their RawConstantValues to create an UInt64 array of all values, sorts both arrays according to the UInt64 value, and finally gets the name from the name-array by doing a BinarySearch in the UInt64-array to find the index of the value we wanted.

    ...and then we throw the fields and the sorted arrays away use that name to find the field again.

    One word: "Ugh!"

    0 讨论(0)
  • 2020-11-22 01:26

    This piece of code should give you a nice little extension method on any enum that lets you retrieve a generic attribute. I believe it's different to the lambda function above because it's simpler to use and slightly - you only need to pass in the generic type.

    public static class EnumHelper
    {
        /// <summary>
        /// Gets an attribute on an enum field value
        /// </summary>
        /// <typeparam name="T">The type of the attribute you want to retrieve</typeparam>
        /// <param name="enumVal">The enum value</param>
        /// <returns>The attribute of type T that exists on the enum value</returns>
        /// <example><![CDATA[string desc = myEnumVariable.GetAttributeOfType<DescriptionAttribute>().Description;]]></example>
        public static T GetAttributeOfType<T>(this Enum enumVal) where T:System.Attribute
        {
            var type = enumVal.GetType();
            var memInfo = type.GetMember(enumVal.ToString());
            var attributes = memInfo[0].GetCustomAttributes(typeof(T), false);
            return (attributes.Length > 0) ? (T)attributes[0] : null;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:26

    Adding my solution for Net Framework and NetCore.

    I used this for my Net Framework implementation:

    public static class EnumerationExtension
    {
        public static string Description( this Enum value )
        {
            // get attributes  
            var field = value.GetType().GetField( value.ToString() );
            var attributes = field.GetCustomAttributes( typeof( DescriptionAttribute ), false );
    
            // return description
            return attributes.Any() ? ( (DescriptionAttribute)attributes.ElementAt( 0 ) ).Description : "Description Not Found";
        }
    }
    

    This doesn't work for NetCore so I modified it to do this:

    public static class EnumerationExtension
    {
        public static string Description( this Enum value )
        {
            // get attributes  
            var field = value.GetType().GetField( value.ToString() );
            var attributes = field.GetCustomAttributes( false );
    
            // Description is in a hidden Attribute class called DisplayAttribute
            // Not to be confused with DisplayNameAttribute
            dynamic displayAttribute = null;
    
            if (attributes.Any())
            {
                displayAttribute = attributes.ElementAt( 0 );
            }
    
            // return description
            return displayAttribute?.Description ?? "Description Not Found";
        }
    }
    

    Enumeration Example:

    public enum ExportTypes
    {
        [Display( Name = "csv", Description = "text/csv" )]
        CSV = 0
    }
    

    Sample Usage for either static added:

    var myDescription = myEnum.Description();
    
    0 讨论(0)
  • 2020-11-22 01:27

    This is a generic implementation using a lambda for the selection

    public static Expected GetAttributeValue<T, Expected>(this Enum enumeration, Func<T, Expected> expression)
        where T : Attribute
    {
        T attribute =
          enumeration
            .GetType()
            .GetMember(enumeration.ToString())
            .Where(member => member.MemberType == MemberTypes.Field)
            .FirstOrDefault()
            .GetCustomAttributes(typeof(T), false)
            .Cast<T>()
            .SingleOrDefault();
    
        if (attribute == null)
            return default(Expected);
    
        return expression(attribute);
    }
    

    Call it like this:

    string description = targetLevel.GetAttributeValue<DescriptionAttribute, string>(x => x.Description);
    
    0 讨论(0)
  • 2020-11-22 01:28

    For some programmer humor, a one liner as a joke:

    public static string GetDescription(this Enum value) => value.GetType().GetMember(value.ToString()).First().GetCustomAttribute<DescriptionAttribute>() is DescriptionAttribute attribute ? attribute.Description : string.Empty;
    

    In a more readable form:

    using System;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection;
    
    public static class EnumExtensions
    {
        // get description from enum:
    
        public static string GetDescription(this Enum value)
        {
            return value.GetType().
                GetMember(value.ToString()).
                First().
                GetCustomAttribute<DescriptionAttribute>() is DescriptionAttribute attribute
                ? attribute.Description
                : throw new Exception($"Enum member '{value.GetType()}.{value}' doesn't have a [DescriptionAttribute]!");
        }
    
        // get enum from description:
    
        public static T GetEnum<T>(this string description) where T : Enum
        {
            foreach (FieldInfo fieldInfo in typeof(T).GetFields())
            {
                if (fieldInfo.GetCustomAttribute<DescriptionAttribute>() is DescriptionAttribute attribute && attribute.Description == description)
                    return (T)fieldInfo.GetRawConstantValue();
            }
    
            throw new Exception($"Enum '{typeof(T)}' doesn't have a member with a [DescriptionAttribute('{description}')]!");
        }
    }
    
    0 讨论(0)
提交回复
热议问题