枚举扩展,感觉用处很大
1 /// <summary> 2 /// 所有的枚举扩展 Created By ZhangQC 2016.08.19 3 /// </summary> 4 public static class EnumExtend 5 { 6 /// <summary> 7 /// 因为使用反射,频繁读取效率不高,故使用静态容器做缓存 Created By ZhangQC 2016.08.19 8 /// </summary> 9 private static readonly Dictionary<Type, KeyValue<bool, Dictionary<int, CustomAttribute>>> EnumCacheDic 10 = new Dictionary<Type, KeyValue<bool, Dictionary<int, CustomAttribute>>>(); 11 12 /// <summary> 13 /// 注册枚举类型Created By ZhangQC 2016.08.19 14 /// </summary> 15 /// <param name="enumType"></param> 16 public static void RegEnumType(Type enumType) 17 { 18 //获取该枚举所有的值 19 var values = Enum.GetValues(enumType); 20 21 //把值转换为键值对《int值,CustomAttribute根据值获取对应的字段特性》 22 var dic = values.Cast<object>() 23 .ToDictionary(j => (int) j, 24 j =>(j.GetType().GetField(j.ToString())).GetCustomAttributes(true) 25 .FirstOrDefault(v=>v is CustomAttribute) as CustomAttribute); 26 var enumCache = 27 new KeyValue<bool, Dictionary<int, CustomAttribute>> ( 28 enumType.GetCustomAttributes(typeof(FlagsAttribute), true).Any(), dic); 29 30 lock (EnumCacheDic) 31 { 32 EnumCacheDic.Add(enumType, enumCache); 33 } 34 } 35 36 /// <summary> 37 /// 根据枚举获取特性 Created By ZhangQC 2016.08.19 38 /// </summary> 39 /// <param name="sender"></param> 40 /// <returns></returns> 41 public static CustomAttribute[] EnumAttr(this Enum sender) 42 { 43 var type = sender.GetType(); 44 lock (EnumCacheDic) 45 { 46 if (!EnumCacheDic.ContainsKey(type)) 47 { 48 RegEnumType(type); 49 } 50 51 var evs = EnumCacheDic[type]; 52 53 if (evs.Key) 54 { 55 //位运算 56 var sv = sender.As(0); 57 58 return evs.Value.Where(j => sv.HasFlag(j.Key)).Select(j => j.Value).ToArray(); 59 } 60 61 //普通枚举 62 return new[] { evs.Value[sender.As(0)] }; 63 } 64 } 65 66 /// <summary> 67 /// 位运算枚举。根据枚举值的和获取对应的枚举 Created By ZhangQC 2016.08.19 68 /// </summary> 69 /// <param name="enumType"></param> 70 /// <param name="num"></param> 71 /// <returns></returns> 72 public static List<string> GetEnumByNum(Type enumType, int num) 73 { 74 var resultList = new List<string>(); 75 //获取该枚举所有的值 76 var values = Enum.GetValues(enumType); 77 var valueList = values.Cast<int>(); 78 79 foreach (var value in valueList) 80 { 81 if (num.HasFlag(value)) 82 { 83 resultList.Add(Enum.GetName(enumType, value)); 84 } 85 } 86 return resultList; 87 } 88 89 90 91 92 /// <summary> 93 /// 获取附加属性 Name 如果是位运算,则返回List. 94 /// </summary> 95 /// <param name="sender"></param> 96 /// <returns></returns> 97 public static string Name(this Enum sender) 98 { 99 var a = EnumAttr(sender); 100 101 return a.Length > 1 ? a.Select(j => j.Name).JoinToString() : a[0].Name; 102 } 103 104 /// <summary> 105 /// 获取附加属性 Value 如果是位运算,则返回List. 106 /// </summary> 107 /// <param name="sender"></param> 108 /// <returns></returns> 109 public static object Value(this Enum sender) 110 { 111 var a = EnumAttr(sender); 112 113 return a.Length > 1 ? a.Select(j => j.Value).ToList() : a[0].Value; 114 } 115 116 /// <summary> 117 /// 获取附加属性 Value1 如果是位运算,则返回List. 118 /// </summary> 119 /// <param name="sender"></param> 120 /// <returns></returns> 121 public static object Value1(this Enum sender) 122 { 123 var a = EnumAttr(sender); 124 125 return a.Length > 1 ? a.Select(j => j.Value1).ToList() : a[0].Value1; 126 } 127 128 /// <summary> 获取附加属性 Value2 如果是位运算,则返回List. 129 /// </summary> 130 /// <param name="sender"></param> 131 /// <returns></returns> 132 public static object Value2(this Enum sender) 133 { 134 var a = EnumAttr(sender); 135 136 return a.Length > 1 ? a.Select(j => j.Value2).ToList() : a[0].Value2; 137 } 138 139 }