Getting attributes of Enum's value

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

    Bryan Rowe and AdamCrawford thanks for your answers!

    But if somebody need method for get Discription (not extension) you can use it:

    string GetEnumDiscription(Enum EnumValue)
            {
                var type = EnumValue.GetType();
                var memInfo = type.GetMember(EnumValue.ToString());
                var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                return (attributes.Length > 0) ? ((DescriptionAttribute)attributes[0]).Description : null;
            }
    

提交回复
热议问题