.NET--------枚举扩展方法(枚举转list,获取枚举描述)
/// <summary>
/// get enum description by name
/// </summary>
/// <typeparam name="T">enum type</typeparam>
/// <param name="enumItemName">the enum name</param>
/// <returns></returns>
public static string GetDescriptionByName<T>(T enumItemName)
{
FieldInfo fi = enumItemName.GetType().GetField(enumItemName.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > )
{
return attributes[].Description;
}
else
{
return enumItemName.ToString();
}
}
/// <summary>
/// 获取描述
/// </summary>
/// <param name="en"></param>
/// <returns></returns>
public static string GetEnumText(this Enum en)
{
FieldInfo fi = en.GetType().GetField(en.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > )
{
return attributes[].Description;
}
else
{
return en.ToString();
}
}
/// <summary>
/// 枚举转list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<EnumModel> ConvertEnumToList<T>()
{
List<EnumModel> list = new List<EnumModel>(); foreach (var e in Enum.GetValues(typeof(T)))
{
EnumModel m = new EnumModel();
m.Value = Convert.ToInt32(e);
m.Description = EnumHelper.GetDescriptionByName((T)e);
list.Add(m);
}
return list;
} public class EnumModel
{
public int Value { get; set; }
public string Description { get; set; }
}
/// <summary>
/// 根据枚举值获得枚举描述
/// </summary>
/// <param name="e"></param>
/// <param name="value"></param>
/// <returns></returns>
public static String GetEnumDesc(this Type e, int? value)
{
FieldInfo[] fields = e.GetFields();
for (int i = , count = fields.Length; i < count; i++)
{
if ((int)System.Enum.Parse(e, fields[i].Name) == value)
{
DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])fields[i].
GetCustomAttributes(typeof(DescriptionAttribute), false);
if (EnumAttributes.Length > )
{
return EnumAttributes[].Description;
}
}
}
return "";
}
.NET--------枚举扩展方法(枚举转list,获取枚举描述)的更多相关文章
- C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素
/// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...
- 枚举扩展方法获取枚举Description
枚举扩展方法 /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="v ...
- .NET(C#):使用反射来获取枚举的名称、值和特性【转】
首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enumMyEnum { AAA, BBB, CCC } 背后的IL是这样的: .class ...
- .NET(C#):使用反射来获取枚举的名称、值和特性
首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enum MyEnum { AAA, BBB, CCC } 背后的IL是这样的: .clas ...
- c#获取枚举
在实际开发项目中,我们定义了一个枚举,往往我们需要在下拉框或其它地方展示枚举.为了加深印象,也为了帮到有需要的人,我写了一个DEMO. 第一步,我们定义一个枚举: /// <summary> ...
- jsp获取枚举的值
Struts2的Action传回页面一个list,页面迭代这个list,获取下拉框的值,获取过来是枚举类型. 在jsp页面获取枚举的常量值和枚举的值的例子如下: jsp页面: <td >状 ...
- C#的扩展方法解析
在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...
- C# 知识回顾 - 扩展方法解析
在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...
- Farseer.net轻量级开源框架 中级篇:常用的扩展方法
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: BasePage.BaseController.BaseHandler.BaseM ...
- WebAPi添加常用扩展方法及思维发散
前言 在WebAPi中我们通常需要得到请求信息中的查询字符串或者请求头中数据再或者是Cookie中的数据,如果需要大量获取,此时我们应该想到封装一个扩展类来添加扩展方法,从而实现简便快捷的获取. We ...
随机推荐
- js原生实现抽奖活动(方形非圆盘)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用C#创建Windows服务 并发布Windows 服务
一.开发环境 操作系统:Windows 10 X64 开发环境:VS2015 编程语言:C# .NET版本:.NET Framework 4.0 目标平台:X86 二.创建Windows Servic ...
- day22
# day22 ## 复习 ```python# 1.内存管理# 引用计数:垃圾回收机制工作原理# -- 引用就 +1 ,释放就 -1 , 当计数为0时,就会被垃圾回收机制回收 # 标记清除:解决循环 ...
- Windows7下Jupyter Notebook使用入门
目录 一.Jupyter简介 二.Jupyter安装 2.1 python 3安装 2.2 Jupyter 安装 三.Jupyter使用示例 四.Jupyter常用命令 五.其他说明 一.Jupyte ...
- object的wait()、notify()、notifyAll()、方法和Condition的await()、signal()方法
wait().notify()和notifyAll()是 Object类 中的方法 从这三个方法的文字描述可以知道以下几点信息: 1)wait().notify()和notifyAll()方法是本地方 ...
- NetSec2019 20165327 Exp6 信息搜集与漏洞扫描
NetSec2019 20165327 Exp6 信息搜集与漏洞扫描 一.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 二.实践内容 1.各种搜索技巧的应用 2.DNS IP注册信息的查询 ...
- Python 用下划线作为变量前缀和后缀指定特殊变量
_xxx 不能用’from module import *’导入 __xxx__ 系统定义名字 __xxx 类中的私有变量名 核心风格:避免用下划线作为变量名的开始. 因为下划线对解释器有特殊的意义, ...
- 【js】【图片瀑布流】js瀑布流显示图片20180315
js实现把图片用瀑布流显示,只需要“jquery-1.11.2.min.js”. js: //瀑布流显示图片 var WaterfallImg = { option: { maxWidth: 850, ...
- 嵌套if-esle语句
C语言自学之嵌套if-esle语句 Dome : 获奖条件为年销售业绩100万以上,并且入职满两年的员工.小明进入公司1年,销售业绩为120万. 在代码编辑器中使用嵌套if-else语句判断小明是否有 ...
- 思科模拟器PacketTracer7-----2台PC通过交叉线互连
实验二—3 实验工具:思科模拟器PacketTracer7(可在思科官网下载,免费) 实验设备: PC两台,交叉线 实验步骤: 一.配置网络拓扑图 二.配置PC0和PC1的IP地址,掩码和网关 四.通 ...