解析定义的枚举

 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("不存在枚举的描述");
}
}

  

c# enum 解析的更多相关文章

  1. Java Enum解析【转】

    Enum用法: 1:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多 ...

  2. Java基础系列-Enum深入解析

    原创文章,转载请标注出处:https://www.cnblogs.com/V1haoge/p/10755129.html 一.概述 枚举就是一个语法糖效果. 定义一个枚举,其实就是定义一个继承抽象类E ...

  3. Java中enum的学习总结

    一.通常的定义常量的方法 public class Sex{ public final static int MALE = 1; public final static int FEMALE=2; } ...

  4. 深度解析开发项目之 03 - enum的使用

    深度解析开发项目之 03 - enum的使用 01 - 在#import和@interface之间定义typedef enum 注意: 默认是0,1,2,3 02 - 定义可以操作的数据类型的属性 0 ...

  5. 设计模式课程 设计模式精讲 8-8 单例设计模式-Enum枚举单例、原理源码解析以及反编译实战

    1 课堂解析 2 代码演练 2.1 枚举类单例解决序列化破坏demo 2.2 枚举类单例解决序列化破坏原理 2.3 枚举类单例解决反射攻击demo 2.4 枚举类单例解决反射攻击原理 3 jad的使用 ...

  6. 【Winform】 Enum逆向解析

    将字符串转换成Enum类型 Enum.Parse:将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象.   名称 说明   Parse(Type, String) 将一个或多个枚举常数 ...

  7. swift的enum模式—对Alamofire入口的解析--数据结构与操作结合的模式

    swift的枚举模式是数据结构与操作结合的模式 1.enum本质是一个类型,可以定义变量: 它定义的变量可以用到其它变量用的的任何地方:函数的输入.输出.成员变量.临时变量等: 这个变量还可以带有附加 ...

  8. EffectiveJava(30) -- 全面解析enum类型

    --在大多数项目中,我们会经常使用int类型来声明final类型的常量,它在不考虑安全的情况下确实能满足我们绝大多数的需求.但是在JDK1.5版本发布之后,声明一组固定的常量组成合法值的类型就建议使用 ...

  9. 【算法】(查找你附近的人) GeoHash核心原理解析及代码实现

    本文地址 原文地址 分享提纲: 0. 引子 1. 感性认识GeoHash 2. GeoHash算法的步骤 3. GeoHash Base32编码长度与精度 4. GeoHash算法 5. 使用注意点( ...

随机推荐

  1. UVa 10817 - Headmaster's Headache(状压DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. 关于使用eclipse maven UpdateProject时报错,无法更新本地仓库的问题解决方案

    在做项目中,需要从同事电脑中把Maven项目copy过来,但是copy的过程中只copy了代码,setting.xml文件和pom.xml,使用eclipse把项目导入,有红色的感叹号提示,由于我没有 ...

  3. VMware12上安装CentOS无法上网问题

    进入/etc/sysconfig/network-scripts目录下,查看有没有ifcfg-XXX的文件(ifcfg-lo除外), 没找到网卡设备,原因:由于Vmware虚拟网卡和Linux兼容问题 ...

  4. 【转】Java中关于WeakReference和WeakHashMap的理解

    新美大的10月11日的笔试中有一道选择题,让选择函数返回结果,代码如下: private static String test(){ String a = new String("a&quo ...

  5. 相机姿态估计(Pose Estimation)

    (未完待续.....) 根据针孔相机模型,相机成像平面一点的像素坐标p和该点在世界坐标系下的3D坐标P有$p=KP$的关系,如果用齐次坐标表示则有: $$dp=KP$$ 其中d是空间点深度(为了将p的 ...

  6. EF Core中关于System.Linq.Dynamic.Core的使用(转载)

    项目中经常用到组合条件查询,根据用户配置的查询条件进行搜索,拼接SQL容易造成SQL注入,普通的LINQ可以用表达式树来完成,但也比较麻烦.有个System.Linq.Dynamic.Core用起来比 ...

  7. Django全面讲解(2/2)

    前戏 Django是Python语言编写的一个全栈式Web框架(其他的还有Tornado,Flask),可帮助我们快速编写一个具有数据库功能,增删改查.后台管理等功能的网站,若不考虑很高的执行速度,去 ...

  8. Oracle中case的第二种用法

    procedure P_GetProVerSingInfo_2018(varFileID in varchar2, p_cr1 out refcontent, p_cr2 out refcontent ...

  9. java web中对json的使用

    一.在Java Web的开发过程中,如果希望调用Java对象转化成JSON对象等操作.则需要引入以下jar包,不然运行时则报错. 1.commons-beanutils.jar 2.commons-c ...

  10. 我的前端工具集(七)div背景网格

    我的前端工具集(七)div背景网格   liuyuhang原创,未经允许禁止转载 目录 我的前端工具集 有时候总觉得div颜色过于白,于是给了10%的灰 但是并不一定能解决问题,因为页面中会有不均衡的 ...