EnumExtentions.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. using NXWMS.Client.Code.Attributes;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace NXWMS.Client.Code.Extends
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. public static class EnumExtentions
  12. {
  13. public static string Display(this Enum t)
  14. {
  15. var t_type = t.GetType();
  16. var fieldName = Enum.GetName(t_type, t);
  17. var attributes = t_type.GetField(fieldName).GetCustomAttributes(false);
  18. var enumDisplayAttribute = attributes.FirstOrDefault(p => p.GetType().Equals(typeof(EnumDisplayAttribute))) as EnumDisplayAttribute;
  19. return enumDisplayAttribute == null ? fieldName : enumDisplayAttribute.Display;
  20. }
  21. public static string ImagePath(this Enum t)
  22. {
  23. var t_type = t.GetType();
  24. var fieldName = Enum.GetName(t_type, t);
  25. var attributes = t_type.GetField(fieldName).GetCustomAttributes(false);
  26. var enumImagePathAttribute = attributes.FirstOrDefault(p => p.GetType().Equals(typeof(EnumImageAttribute))) as EnumImageAttribute;
  27. return enumImagePathAttribute == null ? fieldName : enumImagePathAttribute.imagePath;
  28. }
  29. }
  30. }