public static class EnumExtension
{
public static string DescriptionAttr<T>(this T source)
{
var fi = source.GetType().GetField(source.ToString());
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0) return attributes[0].Description;
else return source.ToString();
}
public static string GetEnumDescription(this Enum value)
{
var fi = value.GetType().GetField(value.ToString());
var attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
}
Friday, March 17, 2017
[Code Sample] Retrieve Enum Description Attribute Value via Extension Method
This extension method allows retrieval of enum description attribute value in c#
Labels:
c#,
code-sample,
extension
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment