EnumHelper.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace OASystem.Domain.Enums
  10. {
  11. public static class EnumHelper
  12. {
  13. //--extension
  14. /// <summary>
  15. /// 获取当前枚举描述
  16. /// </summary>
  17. /// <param name="enumValue"></param>
  18. /// <returns></returns>
  19. public static string GetEnumDescription(this Enum enumValue)
  20. {
  21. try
  22. {
  23. Type type = enumValue.GetType();
  24. MemberInfo[] memInfo = type.GetMember(enumValue.ToString());
  25. if (null != memInfo && memInfo.Length > 0)
  26. {
  27. object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
  28. if (null != attrs && attrs.Length > 0)
  29. return ((DescriptionAttribute)attrs[0]).Description;
  30. }
  31. return enumValue.ToString();
  32. }
  33. catch (Exception)
  34. {
  35. return "";
  36. }
  37. }
  38. //根据值获取枚举方法
  39. public static Enum GetEnumByValue(Type enumType, string value)
  40. {
  41. return Enum.Parse(enumType, value) as Enum;
  42. }
  43. /// <summary>
  44. /// 根据Key获取枚举描述
  45. /// </summary>
  46. /// <param name="en"></param>
  47. /// <returns></returns>
  48. public static string GetDescription(this System.Enum en)
  49. {
  50. Type type = en.GetType();
  51. MemberInfo[] memInfo = type.GetMember(en.ToString());
  52. if (memInfo != null && memInfo.Length > 0)
  53. {
  54. object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
  55. if (attrs != null && attrs.Length > 0)
  56. return ((DescriptionAttribute)attrs[0]).Description;
  57. }
  58. return en.ToString();
  59. }
  60. /// <summary>
  61. /// 根据枚举类型得到其所有的 值 与 枚举定义Description属性 的集合
  62. /// </summary>
  63. /// <param name="enumType"></param>
  64. /// <returns></returns>
  65. public static NameValueCollection GetNVCFromEnumValue(Type enumType)
  66. {
  67. NameValueCollection nvc = new NameValueCollection();
  68. Type typeDescription = typeof(DescriptionAttribute);
  69. System.Reflection.FieldInfo[] fields = enumType.GetFields();
  70. string strText = string.Empty;
  71. string strValue = string.Empty;
  72. foreach (FieldInfo field in fields)
  73. {
  74. if (field.FieldType.IsEnum)
  75. {
  76. strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
  77. object[] arr = field.GetCustomAttributes(typeDescription, true);
  78. if (arr.Length > 0)
  79. {
  80. DescriptionAttribute aa = (DescriptionAttribute)arr[0];
  81. strText = aa.Description;
  82. }
  83. else
  84. {
  85. strText = "";
  86. }
  87. nvc.Add(strValue, strText);
  88. }
  89. }
  90. return nvc;
  91. }
  92. }
  93. }