.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 ...
随机推荐
- 使用Keepalived配置主从热备实现Nginx高可用(HA)
Keepalived 简要介绍 Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前端服务 ...
- vue入门练习(一)
1.安装node,webpack node -v //查看已安装版本 npm install -g webpack //安装webpack npm install -g webpack-dev-se ...
- loadrunner中JavaVuser脚本的编写
1.环境准备: LoadRunner11----->对应JDK1.6版本(32位) LoadRunner12----->对应JDK1.7版本(32位) (一).JDK下载安装完成 ...
- OpenGL.Qt532.cube
1.官方的例子(安装好代源码的Qt532就有该例子) E:\Project_Qt532\Official_Examples\opengl\cube E:\Project_Qt532\Official_ ...
- 干货 | 揭秘如何增加listing多个类目节点
流量是电商销售的必要因素,可以说,任何成功的电商平台都离不开流量.亚马逊listing优化做得好,不仅能提高产品的曝光率,还能提升转换率,而好的类目可以吸引大的流量.帮你快速爬升. 首先我们来了解一下 ...
- 关于Oracle单行函数的讲解
单行函数:对单个数值进行操作,并返回一个值. 分类:1.字符函数 1)concat(a,b) 拼接a,b两个字符串数据 2)initcap(x) 将每个单词x首字母大写 3)low ...
- Apache ant
配置步骤:1,安装JDK,设定下面的环境变量: 添加JAVA_HOME:C:\Program Files\Java\jdk1.7.0_07 JDK版本不同目录名不同Path:追加“;%JAVA_HOM ...
- php(三)使用thinkphp操作数据库
1.数据库设置 在项目D:\workspaces\phpDemo01\helloworldProject\Common\Conf\config.php配置: <?php return array ...
- 基于gensim的LDA主题模型实现 一键式函数打包
def genlda(textlist,n): ticks = str(time.time()).replace('.','')[-6:-1] nn=str(n) dictionary = corpo ...
- PAT 1116 Come on! Let's C
1116 Come on! Let's C (20 分) "Let's C" is a popular and fun programming contest hosted b ...