using System.ComponentModel.DataAnnotations;
public static void Main()
Console.WriteLine("Value: " + ETypes.Apple);
Console.WriteLine(ETypes.Apple.GetEnumName());
Console.WriteLine(ETypes.Apple.GetEnumDescription());
Console.WriteLine(ETypes.Apple.GetEnumDescription());
[Display(Name = "SMTP", Description = "Simple Mail Transfer Protocol")]
[Display(Name = "SMTP/POP3", Description = "Post Office Protocol 3")]
public static class EnumExt {
private static TAttribute GetAttribute<TAttribute>(Enum e) where TAttribute : Attribute
var name = Enum.GetName(type, e);
return type.GetField(name)
.GetCustomAttributes(false)
private static DisplayAttribute GetEnumAttribute(this Enum e) => GetAttribute<DisplayAttribute>(e);
public static string GetEnumName(this Enum e)
var attribute = GetEnumAttribute(e);
returnValue = attribute != null ? attribute.Name : string.Empty;
$"Could not read the 'Name' in the 'Display' attribute from the enum type '{e.GetType().Name}'. Please provide a name for the value '{e}'.";
throw new Exception(message, ex);
public static string GetEnumDescription(this Enum e)
var attribute = GetEnumAttribute(e);
returnValue = attribute.Description;
$"Could not read the 'Description' in the 'Display' attribute from the enum type '{e.GetType().Name}'. Please provide a description for the value '{e}'.";
throw new Exception(message, ex);
public static string GetEnumDisplay(this Enum e)
var returnValue = e.ToString();
var attr = GetAttribute<DisplayAttribute>(e);
if (null != attr) returnValue = attr.GetName();
Console.WriteLine(ex.Message);