EnumExtentions.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using NXWMS.Client.Code.Attributes;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. namespace NXWMS.Client.Code.Extends
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public static class EnumExtentions
  14. {
  15. public static string Display(this Enum t)
  16. {
  17. var t_type = t.GetType();
  18. var fieldName = Enum.GetName(t_type, t);
  19. var attributes = t_type.GetField(fieldName).GetCustomAttributes(false);
  20. var enumDisplayAttribute = attributes.FirstOrDefault(p => p.GetType().Equals(typeof(EnumDisplayAttribute))) as EnumDisplayAttribute;
  21. return enumDisplayAttribute == null ? fieldName : enumDisplayAttribute.Display;
  22. }
  23. public static string ImagePath(this Enum t)
  24. {
  25. var t_type = t.GetType();
  26. var fieldName = Enum.GetName(t_type, t);
  27. var attributes = t_type.GetField(fieldName).GetCustomAttributes(false);
  28. var enumImagePathAttribute = attributes.FirstOrDefault(p => p.GetType().Equals(typeof(EnumImageAttribute))) as EnumImageAttribute;
  29. return enumImagePathAttribute == null ? fieldName : enumImagePathAttribute.imagePath;
  30. }
  31. public static string Description(this Enum t)
  32. {
  33. string value = t.ToString();
  34. FieldInfo field = t.GetType().GetField(value);
  35. object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
  36. if (objs == null || objs.Length == 0)
  37. return value;
  38. DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
  39. return descriptionAttribute.Description;
  40. }
  41. }
  42. }