Getting attributes of Enum's value

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

    This should do what you need.

    var enumType = typeof(FunkyAttributesEnum);
    var memberInfos = enumType.GetMember(FunkyAttributesEnum.NameWithoutSpaces1.ToString());
    var enumValueMemberInfo = memberInfos.FirstOrDefault(m => m.DeclaringType == enumType);
    var valueAttributes = 
          enumValueMemberInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
    var description = ((DescriptionAttribute)valueAttributes[0]).Description;
    

提交回复
热议问题