Skip to content

Instantly share code, notes, and snippets.

@programmation
Last active June 28, 2017 00:29
Show Gist options
  • Save programmation/bbdc368b0f0498326cc524f1e776501d to your computer and use it in GitHub Desktop.
Save programmation/bbdc368b0f0498326cc524f1e776501d to your computer and use it in GitHub Desktop.
public static class EnumExtensions
{
public static string Description(this Enum e)
{
var type = e.GetType();
var name = Enum.GetName(type, e);
var memberInfo = type.GetTypeInfo()
.DeclaredMembers
.Where(p => p.Name == name)
.FirstOrDefault();
var attribute = memberInfo != null
? memberInfo.GetCustomAttributes(true).Where(a => a.GetType() == typeof(DescriptionAttribute)).FirstOrDefault() as DescriptionAttribute
: null;
return attribute != null ? attribute.Description : name;
}
public static string Description(this IEnumerable<Enum> el, string separator = ",")
{
return String.Join(separator, el.Select(x => x.Description()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment