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 <
Taking advantage of some of the newer C# language features, you can reduce the line count:
public static TAttribute GetEnumAttribute(this Enum enumVal) where TAttribute : Attribute
{
var memberInfo = enumVal.GetType().GetMember(enumVal.ToString());
return memberInfo[0].GetCustomAttributes(typeof(TAttribute), false).OfType().FirstOrDefault();
}
public static string GetEnumDescription(this Enum enumValue) => enumValue.GetEnumAttribute()?.Description ?? enumValue.ToString();