c# enum 解析
解析定义的枚举
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 解析的更多相关文章
- Java Enum解析【转】
Enum用法: 1:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多 ...
- Java基础系列-Enum深入解析
原创文章,转载请标注出处:https://www.cnblogs.com/V1haoge/p/10755129.html 一.概述 枚举就是一个语法糖效果. 定义一个枚举,其实就是定义一个继承抽象类E ...
- Java中enum的学习总结
一.通常的定义常量的方法 public class Sex{ public final static int MALE = 1; public final static int FEMALE=2; } ...
- 深度解析开发项目之 03 - enum的使用
深度解析开发项目之 03 - enum的使用 01 - 在#import和@interface之间定义typedef enum 注意: 默认是0,1,2,3 02 - 定义可以操作的数据类型的属性 0 ...
- 设计模式课程 设计模式精讲 8-8 单例设计模式-Enum枚举单例、原理源码解析以及反编译实战
1 课堂解析 2 代码演练 2.1 枚举类单例解决序列化破坏demo 2.2 枚举类单例解决序列化破坏原理 2.3 枚举类单例解决反射攻击demo 2.4 枚举类单例解决反射攻击原理 3 jad的使用 ...
- 【Winform】 Enum逆向解析
将字符串转换成Enum类型 Enum.Parse:将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象. 名称 说明 Parse(Type, String) 将一个或多个枚举常数 ...
- swift的enum模式—对Alamofire入口的解析--数据结构与操作结合的模式
swift的枚举模式是数据结构与操作结合的模式 1.enum本质是一个类型,可以定义变量: 它定义的变量可以用到其它变量用的的任何地方:函数的输入.输出.成员变量.临时变量等: 这个变量还可以带有附加 ...
- EffectiveJava(30) -- 全面解析enum类型
--在大多数项目中,我们会经常使用int类型来声明final类型的常量,它在不考虑安全的情况下确实能满足我们绝大多数的需求.但是在JDK1.5版本发布之后,声明一组固定的常量组成合法值的类型就建议使用 ...
- 【算法】(查找你附近的人) GeoHash核心原理解析及代码实现
本文地址 原文地址 分享提纲: 0. 引子 1. 感性认识GeoHash 2. GeoHash算法的步骤 3. GeoHash Base32编码长度与精度 4. GeoHash算法 5. 使用注意点( ...
随机推荐
- 解决vuex requires a Promise polyfill in this browser问题
造成这种现象的原因归根究底就是浏览器对ES6中的promise无法支持,因此需要通过引入babel-polyfill来是我们的浏览器正常使用es6的功能 首先通过npm来安装: npm install ...
- 配置git
https://blog.csdn.net/qq_34446663/article/details/81106018
- HDU 1029 Ignatius and the Princess IV (map的使用)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/10 ...
- Android平台上PMEM的使用及Platform设备注册(二)
三.注册PMEM设备 这里我们除了描述PMEM设备,还将注册一个拥有memory空间和IRQ资源的示例设备example_device. 对于example_device,定义如下结构体: stati ...
- Java IP白名单相关工具类
关于设置IP白名单相关的一些方法,整理,记录了一下. package com.tools.iptool; import java.util.ArrayList; import java.util.Ha ...
- 系统优化怎么做-JVM优化之开篇
大家好,这里是「聊聊系统优化 」,并在下列地址同步更新 博客园:http://www.cnblogs.com/changsong/ 知乎专栏:https://zhuanlan.zhihu.com/yo ...
- Oracle中case的第二种用法
procedure P_GetProVerSingInfo_2018(varFileID in varchar2, p_cr1 out refcontent, p_cr2 out refcontent ...
- ORCLE10安装常见配置问题-oui.exe停止工作
其实这是一个在安装过程中很常见的问题,之前小编说过关于甲骨文的软件用起来都很强大,但是大腕出厂,出场费是很高的,就像甲骨文的软件使用的话对于他的安装和配置的换将也是很挑剔的,出现这个问题就是因为安装文 ...
- 初学JavaScript从入门到放弃(一)JavaScript介绍、变量、数据类型
一.JavaScript介绍 1.JavaScript:轻量级的客户端脚本语音 2.目前js已经不仅仅是客户语音,基于NODE可以做服务器端程序,所以Javascript是全栈编程语音 3.js及部分 ...
- java servlet数据库查询并将数据显示到jsp页面
需要的jar包:mysql-connector-java.jar build path只是个jar包的引用,部署的时候想不丢包最好还是手动拷贝到对应项目的lib文件下. 在try{}中定义的变量为局部 ...