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. iOS开发之网络数据解析--中文输出

    对于服务器返回的数据,解析之后直接打印,如果数据中原本有中文,可能会出现中文乱码的结果: 为了避免这个问题,可以通过类别来重写系统和打印相关的方法. 步骤: 1.新建文件名:Foundation+Lo ...

  2. 《慕客网:IOS-动画入门》学习笔记

    新建Cocoa Touch Class,语言是swift 然后继续为界面添加一个普通的View Controller,并且添加前面视图的静态table的转向剪头指向这个View Controller, ...

  3. 通知中心NSNotificationCenter的使用

    通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...

  4. android 之 桌面的小控件AppWidget

    AppWidget是创建的桌面窗口小控件,在这个小控件上允许我们进行一些操作(这个视自己的需要而定).作为菜鸟,我在这里将介绍一下AppWeight的简单使用. 1.在介绍AppWidget之前,我们 ...

  5. linux ssh更换默认的22端口

    1.修改配置文件:/etc/ssh/sshd_config 2.先将Port 22 前面的 # 号去掉,并另起一行.如定义SSH端口号为26611 ,则输入 3.修改完毕后,重启SSH服务,并退出当前 ...

  6. 在类库项目中使用log4net(RollingFileAppender)记录日志

    1.创建解决方案 2.创建类库项目 3.根据需要修改命名空间,修改(和/或)添加类到类库 4.引用log4net 5.类库项目根目录下创建leg4net配置文件,如D3CallTriggerPlugi ...

  7. Effetive Java 22 Favor static member classes over nonstatic

    Nested class types Usage and remark Advantage Disadvantage static member classes Use for public help ...

  8. 烂泥:为KVM虚拟机添加网卡

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 前几篇文章介绍了有关KVM安装虚拟机以及如何给虚拟机添加硬盘,今天我们再来介绍下有关如何给KVM虚拟机添加网卡. 给KVM虚拟机添加网卡,可以分为两种形 ...

  9. 树形dp--hdu 3534 Tree

    Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. nagios 自定义插件demo

    #!/bin/bash loadavg=$( uptime | awk -F: '{print $4}' | xargs ) load1int=$( ) load5int=$( ) load15int ...