Last active
June 28, 2017 00:29
-
-
Save programmation/bbdc368b0f0498326cc524f1e776501d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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