C#获取枚举描述代码
public class MusterEnum
{
/// 获取枚举的描述信息
/// </summary>
/// <param name="e">传入枚举对象</param>
/// <returns>得到对应描述信息</returns>
public String GetEnumDesc(Enum e)
{
FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
if (EnumInfo == null)
{
return "";
}
DescriptionAttribute[] EnumAttributes
= (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (EnumAttributes.Length > 0)
{
return EnumAttributes[0].Description;
}
return e.ToString();
} /// <summary>
/// 将含有描述信息的枚举绑定到列表控件中
/// </summary>
/// <param name="listControl"></param>
/// <param name="enumType"></param>
private Dictionary<string, string> BindDesEnumToListControl(System.Type enumType)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (object enumValue in Enum.GetValues(enumType))
{
Enum e = (Enum)enumValue;
dic.Add(((int)enumValue).ToString(), GetEnumDesc(e));
}
return dic;
}
}

还有一个是根据传入的枚举的数字索引直接获取

public static string GetEnumDesc<T>(Object obj)
{
obj = (T)obj;
if (obj == null) throw new ArgumentNullException("参数不能为null");
if (!obj.GetType().IsEnum) throw new Exception("参数类型不正确");
FieldInfo fieldinfo = obj.GetType().GetField(obj.ToString()); string str = string.Empty;
Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs != null && objs.Length != 0)
{
DescriptionAttribute des = (DescriptionAttribute)objs[0];
str = des.Description;
}
return str;
}
C#获取枚举描述代码的更多相关文章
- .NET--------枚举扩展方法(枚举转list,获取枚举描述)
/// <summary> /// get enum description by name /// </summary> /// <typeparam name=&qu ...
- .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法
一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...
- C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素
/// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...
- 【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类
关键代码: using System; using System.Collections; using System.Collections.Generic; using System.Compone ...
- c#获取枚举
在实际开发项目中,我们定义了一个枚举,往往我们需要在下拉框或其它地方展示枚举.为了加深印象,也为了帮到有需要的人,我写了一个DEMO. 第一步,我们定义一个枚举: /// <summary> ...
- C#通过反射进行枚举描述相关操作
C#可以通过反射,来获取枚举的描述信息或通过描述信息获取到指定类型的枚举 /// <summary> /// 获取枚举描述 /// </summary> /// <par ...
- C# 读取枚举描述信息实例
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...
- .net工具类 获取枚举类型的描述
一般情况我们会用枚举类型来存储一些状态信息,而这些信息有时候需要在前端展示,所以需要展示中文注释描述. 为了方便获取这些信息,就封装了一个枚举扩展类. /// <summary> /// ...
- 枚举Enum转换为List,获取枚举的描述
代码: public class EnumberHelper { public static List<EnumberEntity> EnumToList<T>() { Lis ...
随机推荐
- 关于tableView中tableHeaderView/tableFooterView/sectionHeader/sectionFooter/contentInset的理解
其实每个人的理解有所不同,找到最有利于自己的理解方式即可.有人把Cell,tableHeaderView,tableFooterView,sectionHeader,sectionFooter这些属性 ...
- android 短信助手demo
关于意图Intent: 显式意图:必须指定要激活的组件的完整包名和类名(应用程序之间耦合在一起) 一般激活自己应用的组件的时候采用显式意图 隐式意图:只需要指定动作和数据就可以(好处是应用程序之间没有 ...
- 写给IOS开发工程师的网页前端入门笔记
前言:作为IOS开发工程师,终会接触到网页前端开发,甚至可能会有 用HTML5开发IOS的app客户端的需求.比如现在上架的app就有比如理财类型的app有的就用HTML开发的,从理财类型的app需求 ...
- C++语言出现的bug
输出语句不管是C语言的printf();还是cout << "" << endl; 在循环语句中会出现一个bug: 下面是不正常的两种情况: 下面是正常的: ...
- nodeJS创建工程
转http://blog.csdn.net/i348018533/article/details/47258449 设置镜像地址 1.通过config命令 npm config set registr ...
- 记录Hibernate的缓存知识
一.Hibernate缓存的作用 Hibernate是一个持久层框架,Hibernate要经常访问物理数据库.为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能,Hibernate的缓 ...
- NSThread基础使用
1.创建和启动线程 一个NSThread对象就代表一条线程; 创建,启动线程 NSThread *thread = [[NSThread alloc] initWithTarget:self ...
- linux搭建一个配置简单的nginx反向代理服务器 2个tomcat
1.我们只要实现访问nginx服务器能跳转到不同的服务器即可,我本地测试是这样的, 在nginx服务器里面搭建了2个tomcat,2个tomcat端口分别是8080和8081,当我输入我nginx服务 ...
- 修改镜像文件EI.CFG
一.EI.cfg说明 Windows 7 安装光盘中存在着 SOURCES\EI.CFG 这样一个配置文件.EI.cfg 是特定于 Windows 安装程序的配置文件,用于确定在安装过程中应该使用哪种 ...
- Oracle常用plsql
String aggr http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php SELECT 'S ...