平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

130 lines
4.5 KiB

  1. using System.Reflection;
  2. namespace SafeCampus.Core.Extension
  3. {
  4. public static partial class Extensions
  5. {
  6. #region 枚举成员转成dictionary类型
  7. /// <summary>
  8. /// 转成dictionary类型
  9. /// </summary>
  10. /// <param name="enumType"></param>
  11. /// <returns></returns>
  12. public static Dictionary<int, string> EnumToDictionary(this Type enumType)
  13. {
  14. Dictionary<int, string> dictionary = new Dictionary<int, string>();
  15. Type typeDescription = typeof(DescriptionAttribute);
  16. FieldInfo[] fields = enumType.GetFields();
  17. int sValue = 0;
  18. string sText = string.Empty;
  19. foreach (FieldInfo field in fields)
  20. {
  21. if (field.FieldType.IsEnum)
  22. {
  23. sValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null));
  24. object[] arr = field.GetCustomAttributes(typeDescription, true);
  25. if (arr.Length > 0)
  26. {
  27. DescriptionAttribute da = (DescriptionAttribute)arr[0];
  28. sText = da.Description;
  29. }
  30. else
  31. {
  32. sText = field.Name;
  33. }
  34. dictionary.Add(sValue, sText);
  35. }
  36. }
  37. return dictionary;
  38. }
  39. /// <summary>
  40. /// 转成dictionary类型
  41. /// </summary>
  42. /// <param name="enumType"></param>
  43. /// <returns></returns>
  44. public static List<EnumModel> EnumToEnumModel(this Type enumType)
  45. {
  46. List<EnumModel> dictionary = new List<EnumModel>();
  47. Type typeDescription = typeof(DescriptionAttribute);
  48. FieldInfo[] fields = enumType.GetFields();
  49. int sValue = 0;
  50. string sText = string.Empty;
  51. foreach (FieldInfo field in fields)
  52. {
  53. if (field.FieldType.IsEnum)
  54. {
  55. sValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null));
  56. object[] arr = field.GetCustomAttributes(typeDescription, true);
  57. if (arr.Length > 0)
  58. {
  59. DescriptionAttribute da = (DescriptionAttribute)arr[0];
  60. sText = da.Description;
  61. }
  62. dictionary.Add(new EnumModel() { Description = sText,Name = field.Name,Value = sValue });
  63. }
  64. }
  65. return dictionary;
  66. }
  67. /// <summary>
  68. /// 枚举成员转成键值对Json字符串
  69. /// </summary>
  70. /// <param name="enumType"></param>
  71. /// <returns></returns>
  72. public static string EnumToDictionaryString(this Type enumType)
  73. {
  74. List<KeyValuePair<int, string>> dictionaryList = EnumToDictionary(enumType).ToList();
  75. var sJson = JsonConvert.SerializeObject(dictionaryList);
  76. return sJson;
  77. }
  78. #endregion
  79. #region 获取枚举的描述
  80. /// <summary>
  81. /// 获取枚举值对应的描述
  82. /// </summary>
  83. /// <param name="enumType"></param>
  84. /// <returns></returns>
  85. public static string GetDescription(this System.Enum enumType)
  86. {
  87. FieldInfo EnumInfo = enumType.GetType().GetField(enumType.ToString());
  88. if (EnumInfo != null)
  89. {
  90. DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  91. if (EnumAttributes.Length > 0)
  92. {
  93. return EnumAttributes[0].Description;
  94. }
  95. }
  96. return enumType.ToString();
  97. }
  98. #endregion
  99. #region 根据值获取枚举的描述
  100. public static string GetDescriptionByEnum<T>(this object obj)
  101. {
  102. var tEnum = System.Enum.Parse(typeof(T), obj.ParseToString()) as System.Enum;
  103. var description = tEnum.GetDescription();
  104. return description;
  105. }
  106. #endregion
  107. }
  108. public class EnumModel
  109. {
  110. /// <summary>
  111. /// 描述
  112. /// </summary>
  113. public string Description { get; set; }
  114. /// <summary>
  115. /// 属性名
  116. /// </summary>
  117. public string Name { get; set; }
  118. /// <summary>
  119. /// 枚举值
  120. /// </summary>
  121. public int Value { get; set; }
  122. }
  123. }