EnumHelper.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace OASystem.Domain.Enums
  9. {
  10. public static class EnumHelper
  11. {
  12. //--extension
  13. /// <summary>
  14. /// 获取当前枚举描述
  15. /// </summary>
  16. /// <param name="enumValue"></param>
  17. /// <returns></returns>
  18. public static string GetEnumDescription(this Enum enumValue)
  19. {
  20. try
  21. {
  22. Type type = enumValue.GetType();
  23. MemberInfo[] memInfo = type.GetMember(enumValue.ToString());
  24. if (null != memInfo && memInfo.Length > 0)
  25. {
  26. object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
  27. if (null != attrs && attrs.Length > 0)
  28. return ((DescriptionAttribute)attrs[0]).Description;
  29. }
  30. return enumValue.ToString();
  31. }
  32. catch (Exception)
  33. {
  34. return "";
  35. }
  36. }
  37. }
  38. }