c# enum 解析
2024-10-21 06:40:56
解析定义的枚举
public enum OrderPaymentStatus
{
/// <summary>
/// 未支付
/// </summary>
[Description("未支付")]
No=1,
/// <summary>
/// 已支付
/// </summary>
[Description("已支付")]
Yes
}
解析类
public static class EnumHelper
{
private static Hashtable enumDesciption = EnumHelper.GetDescriptionContainer(); public static string ToDescription(this Enum value)
{
if (value == null)
{
return "";
}
Type type = value.GetType();
string name = Enum.GetName(type, value);
return EnumHelper.GetDescription(type, name);
} public static Dictionary<int, string> ToDescriptionDictionary<TEnum>()
{
Type typeFromHandle = typeof(TEnum);
Array values = Enum.GetValues(typeFromHandle);
Dictionary<int, string> dictionary = new Dictionary<int, string>();
foreach (Enum item in values)
{
dictionary.Add(Convert.ToInt32(item), item.ToDescription());
}
return dictionary;
} public static Dictionary<int, string> ToDictionary<TEnum>()
{
Type typeFromHandle = typeof(TEnum);
Array values = Enum.GetValues(typeFromHandle);
Dictionary<int, string> dictionary = new Dictionary<int, string>();
foreach (Enum item in values)
{
dictionary.Add(Convert.ToInt32(item), item.ToString());
}
return dictionary;
} private static bool IsIntType(double d)
{
return (double)(int)d != d;
} private static Hashtable GetDescriptionContainer()
{
EnumHelper.enumDesciption = new Hashtable();
return EnumHelper.enumDesciption;
} private static void AddToEnumDescription(Type enumType)
{
EnumHelper.enumDesciption.Add(enumType, EnumHelper.GetEnumDic(enumType));
} private static Dictionary<string, string> GetEnumDic(Type enumType)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
FieldInfo[] fields = enumType.GetFields();
FieldInfo[] array = fields;
foreach (FieldInfo fieldInfo in array)
{
if (fieldInfo.FieldType.IsEnum)
{
object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
dictionary.Add(fieldInfo.Name, ((DescriptionAttribute)customAttributes[0]).Description);
}
}
return dictionary;
} private static string GetDescription(Type enumType, string enumText)
{
if (string.IsNullOrEmpty(enumText))
{
return null;
}
if (!EnumHelper.enumDesciption.ContainsKey(enumType))
{
EnumHelper.AddToEnumDescription(enumType);
}
object obj = EnumHelper.enumDesciption[enumType];
if (obj != null && !string.IsNullOrEmpty(enumText))
{
Dictionary<string, string> dictionary = (Dictionary<string, string>)obj;
return dictionary[enumText].Split('|')[0];
}
throw new ApplicationException("不存在枚举的描述");
}
}
最新文章
- 测试dns
- web前端之HTML中元素的区分
- (转) The major advancements in Deep Learning in 2016
- Open Audio Library
- CTF中那些脑洞大开的加密(1)
- 【深入Java虚拟机】之一:Java内存模型与内存溢出
- (原)String、StringBuilder、StringBuffer作为形参
- FastMM配置文件详解
- android使用微软雅黑字体
- UITableview xib里面 cell 按钮的回调
- Bag Problem
- shell 外部传入jmeter脚本线程数,rampUp时间,持续运行时间
- Spring + Tomcat 启动报错java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
- Windows文件居然有解锁一说,并且还会引起SignTool Error,真是昏倒!
- Codeforces Round #350 (Div. 2)解题报告
- 怎么做fastreport使用离线数据源
- Bill Total Value
- hibernate---一对一双向外键关联 (重要)
- eval()和$.parseJSON()注意事项
- idea 设置选中代码得背景颜色
热门文章
- textarea使换行变顿号
- 可枚举接口的知识点(IEnumerable 接口)要使用foreach,就必须实现可枚举接口
- oracle 导出空表问题
- openmax component类的继承关系
- 使用 JavaScript 实现名为 flatten(input) 的函数,可以将传入的 input 对象(Object 或者 Array)进行扁平化处理并返回结果
- insert插入数据
- 网站程序application, session, cookie区别的一点小总结
- 关于Date的冷门知识记录
- .Net core 下Swagger如何隐藏接口的显示
- hibernate的报错信息a different object with the same identifier value was already associated with the session解决办法