|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- 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();
- }
-
-
- }
- }
|