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. UITabBarController的创建等基本方法

    #import "AppDelegate.h" @interface AppDelegate () <UITabBarControllerDelegate> @end ...

  2. iOS-代理反向传值<转>

    在上篇博客 iOS代理协议 中,侧重解析了委托代理协议的概念等,本文将侧重于它们在开发中的应用. 假如我们有一个需求如下:界面A上面有一个button.一个label.从界面A跳转到界面B,在界面B的 ...

  3. 高质量c/c++里的strcpy()

    已知strcpy函数的原型是:        char * strcpy(char * strDest,const char * strSrc);    1.不调用库函数,实现strcpy函数.    ...

  4. WebApi 使用PUT和DELETE时报405的问题

    最近两天写了个项目,里面有一个接口是用谓词delete接收请求. 本地完全没问题,但是当发布到服务器上之后(IIS7.5),就报出 405.0 - Method Not Allowed 很明显是配置问 ...

  5. PHP模拟发送POST请求之五curl基本使用和多线程优化

    今天来介绍PHP模拟发送POST请求的重型武器——cURL函数库的使用和其多线程的优化方法. 说起cURL函数,可谓是老生常谈,但网上许多资料都在关键部分语焉不详,列出一大堆手册上的东西,搞得我入门时 ...

  6. 斐波那契数列 递归 尾递归 递推 C++实现

    ==================================声明================================== 本文原创,转载请注明作者和出处,并保证文章的完整性(包括本 ...

  7. HTML实体对照表

    HTML开发特殊字符是没办法原样输出的,必须用到实体,为了以后查看方便,收藏一下实体对照表是必要的,另外,使用<xmp></xmp>标签可以原样输出,当然,也包括特殊字符啦! ...

  8. poj1125&zoj1082Stockbroker Grapevine(Floyd算法)

    Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Description Stockbrokers are known to ...

  9. 【Android 基础】Android中全屏或者取消标题栏

    先介绍去掉标题栏的方法: 第一种:也一般入门的时候经常使用的一种方法 requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 注意这句一定要写在se ...

  10. 【CSS】颜色码对照表

    英文代码 形像颜色 HEX格式 RGB格式 LightPink 浅粉色 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 Crimson 猩红 #DC14 ...