Guest User

Untitled

a guest
Jul 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public static string ToDisplayString(this Enum value)
  2. {
  3. if (value == null)
  4. {
  5. throw new ArgumentNullException("value");
  6. }
  7.  
  8. string description = value.ToString();
  9. EnumDescriptionAttribute[] attributes = (EnumDescriptionAttribute[])value.GetType().GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
  10.  
  11. if (attributes != null && attributes.Length > 0)
  12. {
  13. Type resourceType = attributes[0].ResourceType;
  14.  
  15. if (resourceType != null)
  16. {
  17. var property = resourceType.GetProperty(description);
  18. if (property != null)
  19. {
  20. var propertyValue = property.GetValue(null);
  21.  
  22. if (propertyValue != null)
  23. {
  24. description = propertyValue.ToString();
  25. }
  26. }
  27. }
  28. }
  29. return description;
  30. }
Add Comment
Please, Sign In to add comment