EnumsHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace XYY.Core.Standard.Mvc
  7. {
  8. public class EnumItem
  9. {
  10. public string Name
  11. {
  12. get; set;
  13. }
  14. public int Value
  15. {
  16. get; set;
  17. }
  18. }
  19. public class EnumItem2
  20. {
  21. public string Name
  22. {
  23. get; set;
  24. }
  25. public int Id
  26. {
  27. get; set;
  28. }
  29. }
  30. public static class EnumsHelper
  31. {
  32. public static List<EnumItem2> GetEnumItemsDes<T>() where T : Enum
  33. {
  34. List<EnumItem2> enumItems = new List<EnumItem2>();
  35. var t = typeof(T);
  36. if (t.IsEnum)
  37. {
  38. var values = t.GetEnumValues();
  39. foreach (var v in values)
  40. {
  41. FieldInfo field = t.GetField(t.GetEnumName(v));
  42. DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
  43. enumItems.Add(new EnumItem2
  44. {
  45. Id = (int)v,
  46. Name = attribute == null ? field.Name : attribute.Description
  47. });
  48. }
  49. }
  50. return enumItems;
  51. }
  52. public static List<EnumItem2> GetEnumItems(string enumName)
  53. {
  54. List<EnumItem2> enumItems = new List<EnumItem2>();
  55. string fileName = System.IO.Path.Combine(AppContext.BaseDirectory, "XYY.Model.Standard.dll");
  56. var ass = Assembly.LoadFile(fileName);
  57. var t = ass.GetType("XYY.Model.Standard." + enumName);
  58. if (t != null)
  59. {
  60. if (t.IsEnum)
  61. {
  62. var values = t.GetEnumValues();
  63. foreach (var v in values)
  64. {
  65. enumItems.Add(new EnumItem2
  66. {
  67. Id = (int)v,
  68. Name = t.GetEnumName(v)
  69. });
  70. }
  71. }
  72. }
  73. return enumItems;
  74. }
  75. public static List<EnumItem> GetEnumItems(Type type)
  76. {
  77. string[] Names = Enum.GetNames(type);
  78. var valueAraay = Enum.GetValues(type);
  79. List<EnumItem> list = new List<EnumItem>();
  80. for (int i = 0; i < Names.Length; i++)
  81. {
  82. list.Add(new EnumItem
  83. {
  84. Name = Names[i],
  85. Value = (int)valueAraay.GetValue(i)
  86. });
  87. }
  88. return list;
  89. }
  90. public static List<EnumItem2> GetEnumItemsDes(string enumName)
  91. {
  92. List<EnumItem2> enumItems = new List<EnumItem2>();
  93. string fileName = System.IO.Path.Combine(AppContext.BaseDirectory, "XYY.Model.Standard.dll");
  94. var ass = Assembly.LoadFile(fileName);
  95. var t = ass.GetType("XYY.Model.Standard." + enumName);
  96. if (t != null)
  97. {
  98. if (t.IsEnum)
  99. {
  100. var values = t.GetEnumValues();
  101. foreach (var v in values)
  102. {
  103. FieldInfo field = t.GetField(t.GetEnumName(v));
  104. DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
  105. enumItems.Add(new EnumItem2
  106. {
  107. Id = (int)v,
  108. Name = attribute == null ? "未知" : attribute.Description
  109. });
  110. }
  111. }
  112. }
  113. return enumItems;
  114. }
  115. }
  116. }