Blame view

netcore/src/Modularity/Common/NCC.Common/Extension/EnumExtensions.cs 6.37 KB
de2bd2f9   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  using NCC.Common.Enum;
  using NCC.Common.Util;
  using NCC.Dependency;
  using NCC.FriendlyException;
  using System;
  using System.Collections.Concurrent;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Reflection;
  
  namespace NCC.Common.Extension
  {
      /// <summary>
      /// 枚举<see cref="Enum"/>的扩展辅助操作方法
      /// </summary>
      [SuppressSniffer]
      public static class EnumExtensions
      {
          // 枚举显示字典缓存
          private static readonly ConcurrentDictionary<Type, Dictionary<int, string>> EnumDisplayValueDict = new();
  
          // 枚举值字典缓存
          private static readonly ConcurrentDictionary<Type, Dictionary<int, string>> EnumNameValueDict = new();
  
          // 枚举类型缓存
          private static ConcurrentDictionary<string, Type> _enumTypeDict = null;
  
          /// <summary>
          /// 获取枚举对象Key与名称的字典(缓存)
          /// </summary>
          /// <param name="enumType"></param>
          /// <returns></returns>
          public static Dictionary<int, string> GetEnumDictionary(Type enumType)
          {
              if (!enumType.IsEnum)
                  throw NCCException.Oh(ErrorCode.D1503);
  
              // 查询缓存
              Dictionary<int, string> enumDic = EnumNameValueDict.ContainsKey(enumType) ? EnumNameValueDict[enumType] : new Dictionary<int, string>();
              if (enumDic.Count == 0)
              {
                  // 取枚举类型的Key/Value字典集合
                  enumDic = GetEnumDictionaryItems(enumType);
  
                  // 缓存
                  EnumNameValueDict[enumType] = enumDic;
              }
              return enumDic;
          }
  
          /// <summary>
          /// 获取枚举对象Key与名称的字典
          /// </summary>
          /// <param name="enumType"></param>
          /// <returns></returns>
          private static Dictionary<int, string> GetEnumDictionaryItems(Type enumType)
          {
              // 获取类型的字段,初始化一个有限长度的字典
              FieldInfo[] enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
              Dictionary<int, string> enumDic = new(enumFields.Length);
  
              // 遍历字段数组获取keyname
              foreach (FieldInfo enumField in enumFields)
              {
                  int intValue = (int)enumField.GetValue(enumType);
                  enumDic[intValue] = enumField.Name;
              }
              return enumDic;
          }
  
          /// <summary>
          /// 获取枚举类型key与描述的字典(缓存)
          /// </summary>
          /// <param name="enumType"></param>
          /// <returns></returns>
          /// <exception cref="Exception"></exception>
          public static Dictionary<int, string> GetEnumDescDictionary(Type enumType)
          {
              if (!enumType.IsEnum)
                  throw NCCException.Oh(ErrorCode.D1503);
  
              // 查询缓存
              Dictionary<int, string> enumDic = EnumDisplayValueDict.ContainsKey(enumType) ? EnumDisplayValueDict[enumType] : new Dictionary<int, string>();
              if (enumDic.Count == 0)
              {
                  // 取枚举类型的Key/Value字典集合
                  enumDic = GetEnumDescDictionaryItems(enumType);
  
                  // 缓存
                  EnumDisplayValueDict[enumType] = enumDic;
              }
              return enumDic;
          }
  
          /// <summary>
          /// 获取枚举类型key与描述的字典(没有描述则获取name
          /// </summary>
          /// <param name="enumType"></param>
          /// <returns></returns>
          /// <exception cref="Exception"></exception>
          private static Dictionary<int, string> GetEnumDescDictionaryItems(Type enumType)
          {
              // 获取类型的字段,初始化一个有限长度的字典
              FieldInfo[] enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
              Dictionary<int, string> enumDic = new(enumFields.Length);
  
              // 遍历字段数组获取keyname
              foreach (FieldInfo enumField in enumFields)
              {
                  int intValue = (int)enumField.GetValue(enumType);
                  var desc = enumField.GetDescriptionValue<DescriptionAttribute>();
                  enumDic[intValue] = desc != null && !string.IsNullOrEmpty(desc.Description) ? desc.Description : enumField.Name;
              }
              return enumDic;
          }
  
          /// <summary>
          /// 从程序集中查找指定枚举类型
          /// </summary>
          /// <param name="assembly"></param>
          /// <param name="typeName"></param>
          /// <returns></returns>
          public static Type TryToGetEnumType(Assembly assembly, string typeName)
          {
              // 枚举缓存为空则重新加载枚举类型字典
              _enumTypeDict ??= LoadEnumTypeDict(assembly);
  
              // 按名称查找
              if (_enumTypeDict.ContainsKey(typeName))
              {
                  return _enumTypeDict[typeName];
              }
              return null;
          }
  
          /// <summary>
          /// 从程序集中加载所有枚举类型
          /// </summary>
          /// <param name="assembly"></param>
          /// <returns></returns>
          private static ConcurrentDictionary<string, Type> LoadEnumTypeDict(Assembly assembly)
          {
              // 取程序集中所有类型
              Type[] typeArray = assembly.GetTypes();
  
              // 过滤非枚举类型,转成字典格式并返回
              Dictionary<string, Type> dict = typeArray.Where(o => o.IsEnum).ToDictionary(o => o.Name, o => o);
              ConcurrentDictionary<string, Type> enumTypeDict = new(dict);
              return enumTypeDict;
          }
  
          /// <summary>
          ///  获取枚举的中文描述
          /// </summary>
          /// <param name="obj"></param>
          /// <returns></returns>
          public static string GetDescription<T>(this T tField) where T : System.Enum
          {
              Type enumType = typeof(T);
              var name = System.Enum.GetName(enumType, tField);
              if (name == null)
                  return string.Empty;
              object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
              if (objs == null || objs.Length == 0)
                  return string.Empty;
              DescriptionAttribute attr = objs[0] as DescriptionAttribute;
              return attr.Description;
          }
  
      }
  }