Getting attributes of Enum's value

后端 未结 25 2541
慢半拍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:08

    I this answer to setup a combo box from an enum attributes which was great.

    I then needed to code the reverse so that I can get the selection from the box and return the enum in the correct type.

    I also modified the code to handle the case where an attribute was missing

    For the benefits of the next person, here is my final solution

    public static class Program
    {
       static void Main(string[] args)
        {
           // display the description attribute from the enum
           foreach (Colour type in (Colour[])Enum.GetValues(typeof(Colour)))
           {
                Console.WriteLine(EnumExtensions.ToName(type));
           }
    
           // Get the array from the description
           string xStr = "Yellow";
           Colour thisColour = EnumExtensions.FromName(xStr);
    
           Console.ReadLine();
        }
    
       public enum Colour
       {
           [Description("Colour Red")]
           Red = 0,
    
           [Description("Colour Green")]
           Green = 1,
    
           [Description("Colour Blue")]
           Blue = 2,
    
           Yellow = 3
       }
    }
    
    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(this Enum value) where T : Attribute
        {
            var type = value.GetType();
            var memberInfo = type.GetMember(value.ToString());
            var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
    
            // check if no attributes have been specified.
            if (((Array)attributes).Length > 0)
            {
                return (T)attributes[0];
            }
            else
            {
                return 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();
            return attribute == null ? value.ToString() : attribute.Description;
        }
    
        /// 
        /// Find the enum from the description attribute.
        /// 
        /// 
        /// 
        /// 
        public static T FromName(this string desc) where T : struct
        {
            string attr;
            Boolean found = false;
            T result = (T)Enum.GetValues(typeof(T)).GetValue(0);
    
            foreach (object enumVal in Enum.GetValues(typeof(T)))
            {
                attr = ((Enum)enumVal).ToName();
    
                if (attr == desc)
                {
                    result = (T)enumVal;
                    found = true;
                    break;
                }
            }
    
            if (!found)
            {
                throw new Exception();
            }
    
            return result;
        }
    }
    

    }

提交回复
热议问题