代码:

public class EnumberHelper
{
public static List<EnumberEntity> EnumToList<T>()
{
List<EnumberEntity> list = new List<EnumberEntity>(); foreach (var e in Enum.GetValues(typeof(T)))
{
EnumberEntity m = new EnumberEntity();
object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
if(objArr!=null && objArr.Length>)
{
DescriptionAttribute da = objArr[] as DescriptionAttribute;
m.Desction = da.Description;
}
m.EnumValue = Convert.ToInt32(e);
m.EnumName = e.ToString();
list.Add(m);
}
return list;
}
} public class EnumberEntity
{
/// <summary>
/// 枚举的描述
/// </summary>
public string Desction { set; get; } /// <summary>
/// 枚举名称
/// </summary>
public string EnumName { set; get; } /// <summary>
/// 枚举对象的值
/// </summary>
public int EnumValue { set; get; }
}

枚举:

public enum QxItem
{
[Description("查看")]
Show = ,
[Description("新增")]
Add = ,
[Description("编辑")]
Edit = ,
}

获取描述:

 /// <summary>
/// 获取枚举的描述
/// </summary>
/// <param name="enumValue"></param>
/// <returns></returns>
public static string GetEnumDescription(Enum enumValue)
{
string value = enumValue.ToString();
FieldInfo field = enumValue.GetType().GetField(value);
//获取描述属性
object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
//当描述属性没有时,直接返回名称
if (objs.Length == )
{
return value;
}
DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[];
return descriptionAttribute.Description;
}

另外一种:http://www.cnblogs.com/wenjian/archive/2009/06/19/1506550.html

枚举Enum转换为List,获取枚举的描述的更多相关文章

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

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

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

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

  3. Java枚举enum以及应用:枚举实现单例模式

    枚举作为一个常规的语言概念,一直到Java5才诞生不得不说有点奇怪,以至于到现在为止很多程序员仍然更喜欢用static final的形式去命名常量而不使用,一般情况下,Java程序员用这种方式去实现枚 ...

  4. c# 枚举enum

    1 定义枚举 enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; 默认情况下,枚举中的每个元素的 ...

  5. 聊一聊Java的枚举enum

    一. 什么是枚举 枚举是一种数据类型,具有集合的一些特点,可以存放多个元素,但存储对象有限且固定,枚举也有比较常见的使用场景,如我们需要表达性别(男.女),颜色(红.黄.蓝),星期(星期一.星期二.. ...

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

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

  7. C# 获取枚举的描述Description

    方法类: public static class EnumExtensions { #region Enum /// <summary> /// 获取枚举变量值的 Description ...

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

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

  9. C#获取枚举描述代码

    public class MusterEnum { /// 获取枚举的描述信息 /// </summary> /// <param name="e">传入枚 ...

随机推荐

  1. IOS NSCharacterSet 去除NSString中的空格

    去除 username中的空格,table newline,nextline 代码如下: NSCharacterSet *whitespace = [NSCharacterSet  whitespac ...

  2. jenkins配置maven

    # 去官网下载maven # 安装 cd /opt wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.5.3/binari ...

  3. 490 - Rotating Sentences

     Rotating Sentences  In ``Rotating Sentences,'' you are asked to rotate a series of input sentences ...

  4. Centos7配置 SNMP服务

    本文转载至:http://blog.51cto.com/5001660/2097212   一.安装yum源安装SNMP软件包 1.更新yum源: yum clean all yum makecach ...

  5. mysql_use_result的使用

    对于每个可以产生一个结果集的命令(比如select.show.describe, explain, check_table等等),发起mysql_query或者mysql_real_query之后,你 ...

  6. 使用正则表达式去除html标签

    不知道大家遇到这话总情况没有,从数据库读取数据,数据参杂着html标记<p>等,在显式的时候控制字符个数,这个时候就会出现页面样式串行,使用正则表达式去除html标记就不会有还这个问题. ...

  7. XSS漏洞攻击原理与解决办法

    转自:http://www.frostsky.com/2011/10/xss-hack/ 对于的用户输入中出现XSS漏洞的问题,主要是由于开发人员对XSS了解不足,安全的意识不够造成的.现在让我们来普 ...

  8. php实现静态化

    PHP站点开发过程中,因为搜索引擎对PHP页面搜鹿和html页面的收录有一定的区别,为了站点的推广或者SEO的须要,要对站点进行一定的静态化.静态化并非页面中没有动画等元素,而是指网页的html代码都 ...

  9. Hive插入数据的几种常用方法

    Hive的几种常见的数据导入方式这里介绍四种:(1).从本地文件系统中导入数据到Hive表:(2).从HDFS上导入数据到Hive表:(3).从别的表中查询出相应的数据并导入到Hive表中:(4).在 ...

  10. Codeforces Round #456 B题

    一.题意 给你一个n和一个k,让你从[1, n]区间内选k个数,这k个数异或和最大. 二.思路 我一开始看到这种题,不自觉地就想到,莫非又要搞很复杂的线段树.主席树?貌似还有些难搞啊.然而事实是:Co ...