终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

185 行
6.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  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.Controls;
  10. namespace BPASmartClient.MessageName.EnumHelp
  11. {
  12. /// <summary>
  13. /// 枚举
  14. /// </summary>
  15. public static class EnumExtensions
  16. {
  17. public static int jishu = 0;
  18. /// <summary>
  19. /// 根据控件-》创建枚举类型
  20. /// </summary>
  21. /// <returns></returns>
  22. public static object CreatEnumType(UIElementCollection list)
  23. {
  24. var currentDomain = AppDomain.CurrentDomain;
  25. var assembly = Assembly.GetExecutingAssembly();
  26. var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assembly!.GetName(), AssemblyBuilderAccess.Run);
  27. var moduleBuilder = assemblyBuilder.DefineDynamicModule(assembly.GetName().Name!);
  28. var em = moduleBuilder.DefineEnum($"Control_{((Convert.ToInt32(jishu) + 1).ToString().PadLeft(4, '0'))}", TypeAttributes.Public, typeof(string));
  29. foreach (var item in list)
  30. {
  31. if (item is System.Windows.Controls.Control e)
  32. {
  33. if (e.Tag != null)
  34. {
  35. em.DefineLiteral(e.Tag.ToString(), e.Tag.ToString());
  36. }
  37. }
  38. }
  39. em.DefineLiteral("空", "空");
  40. Type finished = em.CreateType()!;
  41. return finished;
  42. //foreach (object o in Enum.GetValues(finished))
  43. //{
  44. // Console.WriteLine($"{finished}.{o} = {(int)o};");
  45. //}
  46. }
  47. /// <summary>
  48. /// 动态创建枚举
  49. /// </summary>
  50. /// <param name="enumDictionary">枚举元素列表</param>
  51. /// <param name="enumName">枚举名</param>
  52. /// <returns>Enum枚举</returns>
  53. public static Enum CreateEnum(Dictionary<string,int> enumDictionary,string enumName = "DefalutEnum")
  54. {
  55. if (enumDictionary == null || enumDictionary.Count <= 0)
  56. return null;
  57. AppDomain currentDomain = AppDomain.CurrentDomain;
  58. AssemblyName aName = new AssemblyName("TempAssembly");
  59. AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(aName,AssemblyBuilderAccess.Run);
  60. ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
  61. if (string.IsNullOrEmpty(enumName))
  62. {
  63. enumName = "DefalutEnum";
  64. }
  65. EnumBuilder eb = mb.DefineEnum(enumName,TypeAttributes.Public,typeof(int));
  66. foreach (var item in enumDictionary)
  67. {
  68. eb.DefineLiteral(item.Key,item.Value);
  69. }
  70. Type finished = eb.CreateType();
  71. Enum eEnum = Activator.CreateInstance(finished) as Enum;
  72. //foreach (object item in Enum.GetValues(eEnum.GetType()))
  73. //{
  74. // Debug.LogError(string.Format("{0}.{1} = {2}", finished, item, ((int)item)));
  75. //}
  76. return eEnum;
  77. }
  78. /// <summary>
  79. /// 动态创建枚举
  80. /// </summary>
  81. /// <param name="enumDictionary">枚举元素列表</param>
  82. /// <param name="enumName">枚举名</param>
  83. /// <returns>Enum枚举</returns>
  84. public static Enum CreateEnum(List<string> enumList,string enumName = "DefalutEnum")
  85. {
  86. if (enumList == null || enumList.Count <= 0)
  87. return null;
  88. AppDomain currentDomain = AppDomain.CurrentDomain;
  89. AssemblyName aName = new AssemblyName("TempAssembly");
  90. AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(aName,AssemblyBuilderAccess.Run);
  91. ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
  92. if (string.IsNullOrEmpty(enumName))
  93. {
  94. enumName = "DefalutEnum";
  95. }
  96. EnumBuilder eb = mb.DefineEnum(enumName,TypeAttributes.Public,typeof(int));
  97. for (int i = 0; i < enumList.Count; i++)
  98. {
  99. eb.DefineLiteral(enumList[i],i);
  100. }
  101. Type finished = eb.CreateType();
  102. Enum eEnum = Activator.CreateInstance(finished) as Enum;
  103. //foreach (object item in Enum.GetValues(eEnum.GetType()))
  104. //{
  105. // Debug.LogError(string.Format("{0}.{1} = {2}", finished, item, ((int)item)));
  106. //}
  107. return eEnum;
  108. }
  109. /// <summary>
  110. /// 根据枚举int值获取枚举名称
  111. /// </summary>
  112. /// <typeparam name="T">枚举类型</typeparam>
  113. /// <param name="status">枚举值</param>
  114. /// <returns></returns>
  115. public static string GetEnumName<T>(this int status)
  116. {
  117. return Enum.GetName(typeof(T), status);
  118. }
  119. /// <summary>
  120. /// 根据枚举名称获取枚举值
  121. /// </summary>
  122. public static T ToEnumValue<T>(string name)
  123. {
  124. return (T)Enum.Parse(typeof(T),name);
  125. }
  126. /// <summary>
  127. /// 获取枚举变量值的 Description 属性
  128. /// </summary>
  129. /// <param name="obj">枚举变量</param>
  130. /// <returns>如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称</returns>
  131. public static string GetDescription(this Enum obj)
  132. {
  133. return GetDescription(obj, false);
  134. }
  135. /// <summary>
  136. /// 获取枚举变量值的 Description 属性
  137. /// </summary>
  138. /// <param name="obj">枚举变量</param>
  139. /// <param name="isTop">是否改变为返回该类、枚举类型的头 Description 属性,而不是当前的属性或枚举变量值的 Description 属性</param>
  140. /// <returns>如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称</returns>
  141. public static string GetDescription(this Enum obj, bool isTop)
  142. {
  143. if (obj == null)
  144. {
  145. return string.Empty;
  146. }
  147. Type enumType = obj.GetType();
  148. DescriptionAttribute dna;
  149. if (isTop)
  150. {
  151. dna = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute));
  152. }
  153. else
  154. {
  155. FieldInfo fi = enumType.GetField(System.Enum.GetName(enumType, obj));
  156. dna = (DescriptionAttribute)Attribute.GetCustomAttribute(
  157. fi, typeof(DescriptionAttribute));
  158. }
  159. if ((dna != null)
  160. && (string.IsNullOrEmpty(dna.Description) == false))
  161. {
  162. return dna.Description;
  163. }
  164. return obj.ToString();
  165. }
  166. }
  167. }