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#获取枚举描述代码的更多相关文章

  1. .NET--------枚举扩展方法(枚举转list,获取枚举描述)

    /// <summary> /// get enum description by name /// </summary> /// <typeparam name=&qu ...

  2. .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法

    一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...

  3. C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素

    /// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...

  4. 【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类

    关键代码: using System; using System.Collections; using System.Collections.Generic; using System.Compone ...

  5. c#获取枚举

    在实际开发项目中,我们定义了一个枚举,往往我们需要在下拉框或其它地方展示枚举.为了加深印象,也为了帮到有需要的人,我写了一个DEMO. 第一步,我们定义一个枚举: /// <summary> ...

  6. C#通过反射进行枚举描述相关操作

    C#可以通过反射,来获取枚举的描述信息或通过描述信息获取到指定类型的枚举 /// <summary> /// 获取枚举描述 /// </summary> /// <par ...

  7. C# 读取枚举描述信息实例

    using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...

  8. .net工具类 获取枚举类型的描述

    一般情况我们会用枚举类型来存储一些状态信息,而这些信息有时候需要在前端展示,所以需要展示中文注释描述. 为了方便获取这些信息,就封装了一个枚举扩展类. /// <summary> /// ...

  9. 枚举Enum转换为List,获取枚举的描述

    代码: public class EnumberHelper { public static List<EnumberEntity> EnumToList<T>() { Lis ...

随机推荐

  1. UnityShader之Shader分类篇【Shader资料2】

    关于ShaderLab,从我个人的理解上来看应该是分为三种类型. 1.Fixed function shader 固定渲染管线Shader,基于用于高级Shader在老显卡无法显示时的Fallback ...

  2. OC语言-06-OC语言-block与protocol

    一.block 1> 基本使用 相当于用来存放代码的代码块 效率高 若没有形参可以省略小括号 2> block与函数的相同点 可以保存代码 可以有返回值 可以有形参 调用方式一样 3> ...

  3. C语言错误之--初始值(低级错误)

    今天犯了一个低级错误,虽然低级,但是也不能忽视,一个低级错误以后可能小则浪费时间和精力,大则酿成整个app的项目bug.    

  4. 使用APUE(UNIX高级编程)源代码

    方法一:1.APUE源代码下载:http://www.apuebook.com/code3e.html2.我保存到了/root下.解压缩:tar -xzvf src.tar.gz3.cd apue.2 ...

  5. MSLocalDB

    今天用MSLocalDB做测试,发现保存的中文都变成了??,基本可以确定是排序规则的问题,LocalDB建库默认使用Latin规则,需要改为Chinese_PRC_CI_AS,为了修改规则,需要先修改 ...

  6. MFC9.0 Outlook控件的标题显示无法修改

    这是我在开发中遇到的问题,现记录下来,以便帮助你们. 不想看废话的可以只看最后三行,但你会错过很多. 俗话说的好啊,"Wise men learn by other men's mistak ...

  7. 使用cxf构建webservice

    一.简介 cxf是apache下的一个开源的,功能齐全的WebService框架.是由Celtix和XFire两个开源框架组合而成.cxf可以帮助我们使用前端编程模型构建和开发服务,如JAX- WS和 ...

  8. centos升级mysql至5.7

    1.备份原数据库 [root@www ~] #mysqldump -u root –p -E –all-database > /home/db-backup.sql 加-E是因为mysqldum ...

  9. MyCat 学习笔记 第十篇.数据分片 之 ER分片

    1 应用场景 这篇来说下mycat中自带的er关系分片,所谓er关系分片即可以理解为有关联关系表之间数据分片.类似于订单主表与订单详情表间的分片存储规则. 本文所说的er分片分为两种: a. 依据主键 ...

  10. eclipse插件本地扩展安装

    (1)在Eclipse 安装路径下新建links 路径. (2) 在links 文件夹内,建立X X X .link 文件,该文件的文件名可随意,但后缀必须是link ,通常推荐该文件的文件名与插件名 ...