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 <
This is a generic implementation using a lambda for the selection
public static Expected GetAttributeValue(this Enum enumeration, Func expression)
where T : Attribute
{
T attribute =
enumeration
.GetType()
.GetMember(enumeration.ToString())
.Where(member => member.MemberType == MemberTypes.Field)
.FirstOrDefault()
.GetCustomAttributes(typeof(T), false)
.Cast()
.SingleOrDefault();
if (attribute == null)
return default(Expected);
return expression(attribute);
}
Call it like this:
string description = targetLevel.GetAttributeValue(x => x.Description);