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
{
///
/// 枚举
///
public static class EnumExtensions
{
public static int jishu = 0;
///
/// 根据控件-》创建枚举类型
///
///
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};");
//}
}
///
/// 动态创建枚举
///
/// 枚举元素列表
/// 枚举名
/// Enum枚举
public static Enum CreateEnum(Dictionary 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;
}
///
/// 动态创建枚举
///
/// 枚举元素列表
/// 枚举名
/// Enum枚举
public static Enum CreateEnum(List 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;
}
///
/// 根据枚举int值获取枚举名称
///
/// 枚举类型
/// 枚举值
///
public static string GetEnumName(this int status)
{
return Enum.GetName(typeof(T), status);
}
///
/// 根据枚举名称获取枚举值
///
public static T ToEnumValue(string name)
{
return (T)Enum.Parse(typeof(T),name);
}
///
/// 获取枚举变量值的 Description 属性
///
/// 枚举变量
/// 如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称
public static string GetDescription(this Enum obj)
{
return GetDescription(obj, false);
}
///
/// 获取枚举变量值的 Description 属性
///
/// 枚举变量
/// 是否改变为返回该类、枚举类型的头 Description 属性,而不是当前的属性或枚举变量值的 Description 属性
/// 如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称
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();
}
}
}