|
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Reflection.Emit;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Controls;
-
- namespace BPASmartClient.MessageName.EnumHelp
- {
- /// <summary>
- /// 枚举
- /// </summary>
- public static class EnumExtensions
- {
- public static int jishu = 0;
- /// <summary>
- /// 根据控件-》创建枚举类型
- /// </summary>
- /// <returns></returns>
- public static object CreatEnumType(UIElementCollection list)
- {
- var currentDomain = AppDomain.CurrentDomain;
- var assembly = Assembly.GetExecutingAssembly();
- var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assembly!.GetName(), AssemblyBuilderAccess.Run);
- var moduleBuilder = assemblyBuilder.DefineDynamicModule(assembly.GetName().Name!);
- var em = moduleBuilder.DefineEnum($"Control_{((Convert.ToInt32(jishu) + 1).ToString().PadLeft(4, '0'))}", TypeAttributes.Public, typeof(string));
- foreach (var item in list)
- {
- if (item is System.Windows.Controls.Control e)
- {
- if (e.Tag != null)
- {
- em.DefineLiteral(e.Tag.ToString(), e.Tag.ToString());
- }
- }
- }
- em.DefineLiteral("空", "空");
- Type finished = em.CreateType()!;
- return finished;
- //foreach (object o in Enum.GetValues(finished))
- //{
- // Console.WriteLine($"{finished}.{o} = {(int)o};");
- //}
- }
-
-
- /// <summary>
- /// 动态创建枚举
- /// </summary>
- /// <param name="enumDictionary">枚举元素列表</param>
- /// <param name="enumName">枚举名</param>
- /// <returns>Enum枚举</returns>
- public static Enum CreateEnum(Dictionary<string,int> enumDictionary,string enumName = "DefalutEnum")
- {
- if (enumDictionary == null || enumDictionary.Count <= 0)
- return null;
-
- AppDomain currentDomain = AppDomain.CurrentDomain;
- AssemblyName aName = new AssemblyName("TempAssembly");
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(aName,AssemblyBuilderAccess.Run);
- ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
- if (string.IsNullOrEmpty(enumName))
- {
- enumName = "DefalutEnum";
- }
- EnumBuilder eb = mb.DefineEnum(enumName,TypeAttributes.Public,typeof(int));
-
- foreach (var item in enumDictionary)
- {
- eb.DefineLiteral(item.Key,item.Value);
- }
-
- Type finished = eb.CreateType();
- Enum eEnum = Activator.CreateInstance(finished) as Enum;
- //foreach (object item in Enum.GetValues(eEnum.GetType()))
- //{
- // Debug.LogError(string.Format("{0}.{1} = {2}", finished, item, ((int)item)));
- //}
- return eEnum;
- }
-
- /// <summary>
- /// 动态创建枚举
- /// </summary>
- /// <param name="enumDictionary">枚举元素列表</param>
- /// <param name="enumName">枚举名</param>
- /// <returns>Enum枚举</returns>
- public static Enum CreateEnum(List<string> enumList,string enumName = "DefalutEnum")
- {
- if (enumList == null || enumList.Count <= 0)
- return null;
-
- AppDomain currentDomain = AppDomain.CurrentDomain;
- AssemblyName aName = new AssemblyName("TempAssembly");
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(aName,AssemblyBuilderAccess.Run);
- ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
- if (string.IsNullOrEmpty(enumName))
- {
- enumName = "DefalutEnum";
- }
- EnumBuilder eb = mb.DefineEnum(enumName,TypeAttributes.Public,typeof(int));
-
- for (int i = 0; i < enumList.Count; i++)
- {
- eb.DefineLiteral(enumList[i],i);
- }
- Type finished = eb.CreateType();
- Enum eEnum = Activator.CreateInstance(finished) as Enum;
- //foreach (object item in Enum.GetValues(eEnum.GetType()))
- //{
- // Debug.LogError(string.Format("{0}.{1} = {2}", finished, item, ((int)item)));
- //}
- return eEnum;
- }
-
- /// <summary>
- /// 根据枚举int值获取枚举名称
- /// </summary>
- /// <typeparam name="T">枚举类型</typeparam>
- /// <param name="status">枚举值</param>
- /// <returns></returns>
- public static string GetEnumName<T>(this int status)
- {
- return Enum.GetName(typeof(T), status);
- }
-
- /// <summary>
- /// 根据枚举名称获取枚举值
- /// </summary>
- public static T ToEnumValue<T>(string name)
- {
- return (T)Enum.Parse(typeof(T),name);
- }
-
-
- /// <summary>
- /// 获取枚举变量值的 Description 属性
- /// </summary>
- /// <param name="obj">枚举变量</param>
- /// <returns>如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称</returns>
- public static string GetDescription(this Enum obj)
- {
- return GetDescription(obj, false);
- }
-
- /// <summary>
- /// 获取枚举变量值的 Description 属性
- /// </summary>
- /// <param name="obj">枚举变量</param>
- /// <param name="isTop">是否改变为返回该类、枚举类型的头 Description 属性,而不是当前的属性或枚举变量值的 Description 属性</param>
- /// <returns>如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称</returns>
- public static string GetDescription(this Enum obj, bool isTop)
- {
- if (obj == null)
- {
- return string.Empty;
- }
- Type enumType = obj.GetType();
- DescriptionAttribute dna;
-
- if (isTop)
- {
- dna = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute));
- }
- else
- {
- FieldInfo fi = enumType.GetField(System.Enum.GetName(enumType, obj));
- dna = (DescriptionAttribute)Attribute.GetCustomAttribute(
- fi, typeof(DescriptionAttribute));
- }
- if ((dna != null)
- && (string.IsNullOrEmpty(dna.Description) == false))
- {
- return dna.Description;
- }
- return obj.ToString();
- }
-
-
- }
- }
|