Advertisement
Guest User

Untitled

a guest
Nov 13th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. public static class EnumHelper
  2. {
  3. /// <summary>
  4. /// Retrieve the description on the enum, e.g.
  5. /// [Description("Bright Pink")]
  6. /// BrightPink = 2,
  7. /// Then when you pass in the enum, it will retrieve the description
  8. /// </summary>
  9. /// <param name="en">The Enumeration</param>
  10. /// <returns>A string representing the friendly name</returns>
  11. public static string GetDescription(Enum en)
  12. {
  13. Type type = en.GetType();
  14.  
  15. MemberInfo[] memInfo = type.GetMember(en.ToString());
  16.  
  17. if (memInfo != null && memInfo.Length > 0)
  18. {
  19. object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
  20.  
  21. if (attrs != null && attrs.Length > 0)
  22. {
  23. return ((DescriptionAttribute)attrs[0]).Description;
  24. }
  25. }
  26.  
  27. return en.ToString();
  28. }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement