版博士V2.0程序
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.
 
 
 
 

213 lines
7.4 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace AssistClient.Utils
  11. {
  12. public static class EnumUtil
  13. {
  14. /// <summary>
  15. /// 动态创建枚举
  16. /// </summary>
  17. /// <param name="enumDictionary">枚举元素列表</param>
  18. /// <param name="enumName">枚举名</param>
  19. /// <returns>Enum枚举</returns>
  20. public static Enum CreateEnum(Dictionary<string, int> enumDictionary, string enumName = "DefalutEnum")
  21. {
  22. if (enumDictionary == null || enumDictionary.Count <= 0)
  23. return null;
  24. AppDomain currentDomain = AppDomain.CurrentDomain;
  25. AssemblyName aName = new AssemblyName("TempAssembly");
  26. AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
  27. ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
  28. if (string.IsNullOrWhiteSpace(enumName))
  29. {
  30. enumName = "DefalutEnum";
  31. }
  32. EnumBuilder eb = mb.DefineEnum(enumName, TypeAttributes.Public, typeof(int));
  33. foreach (var item in enumDictionary)
  34. {
  35. eb.DefineLiteral(item.Key, item.Value);
  36. }
  37. Type finished = eb.CreateType();
  38. return (Enum)Enum.Parse(finished, enumName);
  39. //ab.Save(aName.Name + ".dll");
  40. //Enum.GetValues(finished)
  41. //Enum eEnum = System.Reflection(finished) as Enum;
  42. //foreach (object item in Enum.GetValues(eEnum.GetType()))
  43. //{
  44. // Debug.LogError(string.Format("{0}.{1} = {2}", finished, item, ((int)item)));
  45. //}
  46. //return eEnum;
  47. }
  48. /// <summary>
  49. /// 动态创建枚举
  50. /// </summary>
  51. /// <param name="enumDictionary">枚举元素列表</param>
  52. /// <param name="enumName">枚举名</param>
  53. /// <returns>Enum枚举</returns>
  54. public static Enum CreateEnum(List<string> enumList, string enumName = "DefalutEnum")
  55. {
  56. if (enumList == null || enumList.Count <= 0)
  57. return null;
  58. AppDomain currentDomain = AppDomain.CurrentDomain;
  59. AssemblyName aName = new AssemblyName("TempAssembly");
  60. AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
  61. ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
  62. if (string.IsNullOrWhiteSpace(enumName))
  63. {
  64. enumName = "DefalutEnum";
  65. }
  66. EnumBuilder eb = mb.DefineEnum(enumName, TypeAttributes.Public, typeof(int));
  67. for (int i = 0; i < enumList.Count; i++)
  68. {
  69. eb.DefineLiteral(enumList[i], i);
  70. }
  71. Type finished = eb.CreateType();
  72. return (Enum)Enum.Parse(finished, enumName);
  73. //Enum eEnum = Reflection.GetDefault(finished) as Enum;
  74. //foreach (object item in Enum.GetValues(eEnum.GetType()))
  75. //{
  76. // Debug.LogError(string.Format("{0}.{1} = {2}", finished, item, ((int)item)));
  77. //}
  78. //return eEnum;
  79. }
  80. /// <summary>
  81. /// 根据枚举的值获取枚举名称
  82. /// </summary>
  83. /// <typeparam name="T">枚举类型</typeparam>
  84. /// <param name="status">枚举的值</param>
  85. /// <returns></returns>
  86. public static string GetEnumName<T>(this int status)
  87. {
  88. return Enum.GetName(typeof(T), status);
  89. }
  90. /// <summary>
  91. /// 获取枚举名称集合
  92. /// </summary>
  93. /// <typeparam name="T"></typeparam>
  94. /// <returns></returns>
  95. public static string[] GetNamesArr<T>()
  96. {
  97. return Enum.GetNames(typeof(T));
  98. }
  99. /// <summary>
  100. /// 将枚举转换成字典集合
  101. /// </summary>
  102. /// <typeparam name="T">枚举类型</typeparam>
  103. /// <returns></returns>
  104. public static Dictionary<string, int> getDic<T>()
  105. {
  106. Dictionary<string, int> resultList = new Dictionary<string, int>();
  107. Type type = typeof(T);
  108. //var strList = GetNamesArr<T>().ToList();
  109. //foreach (string key in strList)
  110. //{
  111. // string val = Enum.Format(type, Enum.Parse(type, key), "d");
  112. // resultList.Add(key, int.Parse(val));
  113. //}
  114. foreach (int value in Enum.GetValues(type))
  115. {
  116. string strName = Enum.GetName(type, value);//获取名称
  117. //string strVaule = value.ToString();//获取值
  118. resultList.Add(strName, value);
  119. }
  120. return resultList;
  121. }
  122. /// <summary>
  123. /// 将枚举转换成字典
  124. /// </summary>
  125. /// <typeparam name="TEnum"></typeparam>
  126. /// <returns></returns>
  127. public static Dictionary<string, int> getDic2<TEnum>()
  128. {
  129. Dictionary<string, int> dic = new Dictionary<string, int>();
  130. Type t = typeof(TEnum);
  131. var arr = Enum.GetValues(t);
  132. foreach (var item in arr)
  133. {
  134. dic.Add(item.ToString(), (int)item);
  135. }
  136. return dic;
  137. }
  138. public static ArrayList GetArrayList<T>()
  139. {
  140. ArrayList list = new ArrayList();
  141. Type type = typeof(T);
  142. //list.Add(new DictionaryEntry("start", "启动按钮"));
  143. foreach (int value in Enum.GetValues(type))
  144. {
  145. string strName = Enum.GetName(type, value);//获取名称
  146. list.Add(new DictionaryEntry(value, strName));
  147. }
  148. return list;
  149. }
  150. /// <summary>
  151. /// 通过Enum的名称转为Enum类型
  152. /// </summary>
  153. /// <typeparam name="T"></typeparam>
  154. /// <param name="codeName"></param>
  155. /// <returns></returns>
  156. public static T Convert2Enum<T>(string codeName)
  157. {
  158. T myEnum = (T)Enum.Parse(typeof(T), codeName);
  159. return myEnum;
  160. }
  161. #region 绑定到ComboBox
  162. /// <summary>
  163. /// 绑定ComboBoxEx数据源到枚举类型
  164. /// </summary>
  165. /// <param name="cmb"></param>
  166. /// <param name="enumType"></param>
  167. /// <param name="selectIndex"></param>
  168. public static void BindToEnumName<T>(ComboBox cmb, Type enumType, T value)
  169. {
  170. cmb.DataSource = Enum.GetNames(enumType);
  171. if(value!=null)
  172. SetSelectedItemToEnum(cmb, value);
  173. }
  174. /// <summary>
  175. /// 获取选择项
  176. /// </summary>
  177. /// <typeparam name="T"></typeparam>
  178. /// <param name="cmb"></param>
  179. /// <returns></returns>
  180. public static T GetSelectedItemToEnum<T>(this ComboBox cmb)
  181. {
  182. return (T)Enum.Parse(typeof(T), cmb.SelectedItem.ToString(), false);
  183. }
  184. /// <summary>
  185. /// 设置选定项
  186. /// </summary>
  187. /// <typeparam name="T"></typeparam>
  188. /// <param name="cmb"></param>
  189. /// <param name="t"></param>
  190. public static void SetSelectedItemToEnum<T>(this ComboBox cmb, T t)
  191. {
  192. cmb.SelectedItem = t.ToString();
  193. }
  194. #endregion
  195. }
  196. }