using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Reflection;

public partial class EnumTest : System.Web.UI.Page
{
    /// <summary>
    /// 调用获得Name,Value,Description方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        List<EnumModel> modelFirst = MethodOne_GetEnumModel<MyEnum>();
        List<EnumModel> modelSecond = MethodTow_GetEnumModel<MyEnum>();
        foreach (EnumModel enumModel in modelFirst)
        {
            Response.Write("枚举Name:" + enumModel.Name + "   লল Value: " + enumModel.Value + "    ললDescription:" + enumModel.Description + "<br/><br/>");
        }
    }

/// <summary>
    /// 用于测试的枚举
    /// </summary>
    public enum MyEnum
    {
        [Description("这是第一个枚举值得描述MyTestA")]
        MyA = 0,

[Description("这是第二个枚举值的描述MyTestB")]
        MyB = 1,

[Description("这是第三个枚举值的描述MyTestC")]
        MyC = 2
    }

/// <summary>
    /// 用于保存枚举值Name,Value,Description的类
    /// </summary>
    public class EnumModel
    {
        public string Description { get; set; }
        public string Name { get; set; }
        public int Value { get; set; }
    }

/// <summary>
    /// MethodOne获取枚举的Name,Value,Description
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public List<EnumModel> MethodOne_GetEnumModel<T>()
    {
        List<EnumModel> listEnumModel = new List<EnumModel>();

#region
        /*
         * 表示类型声明,类类型,接口类型,数组类型,值类型,枚举类型,类型参数,泛型类型定义,以及开放或封闭构造的泛型。
         */
        #endregion
        Type type = typeof(T);
        #region
        /*
         * FieldInfo http://msdn.microsoft.com/zh-cn/library/system.reflection.fieldinfo(v=vs.95).aspx
         *
         */
        #endregion
        FieldInfo[] fieldInfos = type.GetFields();
        foreach (FieldInfo fieldInfo in fieldInfos)
        {
            EnumModel enumModel = new EnumModel();
            if (!fieldInfo.IsSpecialName)
            {
                enumModel.Name = fieldInfo.Name;
                enumModel.Value = ((T)Enum.Parse(type, fieldInfo.Name)).GetHashCode();

DescriptionAttribute[] enumAttributeList = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (enumAttributeList != null && enumAttributeList.Length > 0)
                {
                    enumModel.Description = enumAttributeList[0].Description;
                }
                else
                {
                    enumModel.Description = fieldInfo.Name;
                }

/*
                 * 下面的方法也可以获得枚举的描述
                     dynamic dy = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                     if (dy != null && dy.Length>0)
                     {
                         enumModel.Description = dy[0].Description;
                     }
                     else
                     {
                         enumModel.Description = fieldInfo.Name;
                     }
                */

listEnumModel.Add(enumModel);
            }
        }
        return listEnumModel;
    }

/// <summary>
    /// MethodTwo获取枚举的Name,Value,Description
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public List<EnumModel> MethodTow_GetEnumModel<T>()
    {
        List<EnumModel> listEnumModel = new List<EnumModel>() { };
        EnumModel enumModel;
        Type type = typeof(T);
        foreach (string item in Enum.GetNames(type))
        {
            enumModel = new EnumModel();
            enumModel.Name = item;
            FieldInfo fieldInfo = type.GetField(item);
            //enumModel.Value = ((T)Enum.Parse(type, item)).GetHashCode();
            enumModel.Value = fieldInfo.GetValue(item).GetHashCode();
            DescriptionAttribute[] enumAttributeList = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (enumAttributeList != null && enumAttributeList.Length > 0)
            {
                enumModel.Description = enumAttributeList[0].Description;
            }
            else
            {
                enumModel.Description = item;
            }

/*
                 * 下面的方法也可以获得枚举的描述
                     dynamic dy = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                     if (dy != null && dy.Length>0)
                     {
                         enumModel.Description = dy[0].Description;
                     }
                     else
                     {
                         enumModel.Description = fieldInfo.Name;
                     }
                */

listEnumModel.Add(enumModel);
        }
        return listEnumModel;
    }

}

获取枚举Name,Value,Description两种方法的更多相关文章

  1. java 获取键盘输入常用的两种方法

    java 获取键盘输入常用的两种方法 方法1: 通过 Scanner Scanner input = new Scanner(System.in); String s = input.nextLine ...

  2. QT中获取选中的radioButton的两种方法(动态取得控件的objectName之后,对名字进行比较)

    QT中获取选中的radioButton的两种方法   QT中要获取radioButton组中被选中的那个按钮,可以采用两种如下两种办法进行: 方法一:采用对象名称进行获取 代码: 1 QRadioBu ...

  3. 使用JavaScript获取URL中的参数(两种方法)

    本文给大家分享两种方法使用js获取url中的参数,其中方法二是使用的正则表达式方法,大家可以根据需要选择比较好的方法,废话不多说了,直接看详细介绍吧. 方法一: //取url参数 var type = ...

  4. QT中获取选中的radioButton的两种方法

    QT中要获取radioButton组中被选中的那个按钮,可以采用两种如下两种办法进行: 方法一:采用对象名称进行获取 代码: 1 QRadioButton* pbtn = qobject_cast&l ...

  5. javascript 获取当前 URL 参数的两种方法

    window.location.host; //返回url 的主机部分,例如:www.xxx.com window.location.hostname; //返回www.xxx.com window. ...

  6. js获取json属性值的两种方法

    1.json.XXX 2.json["XXX"] 第二种方法使用场景,当属性值是变量时.如图所示:

  7. VC++实现获取文件占用空间大小的两种方法(非文件大小)

    // GetFileSpaceSize.cpp : Defines the entry point for the console application. // /***************** ...

  8. 【Jquery】jQuery获取URL參数的两种方法

    jQuery获取URL參数的关键是获取到URL,然后对URL进行过滤处理,取出參数. location.href是取得URL.location.search是取得URL"?"之后的 ...

  9. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

随机推荐

  1. 善待Redis里的数据--Unable to validate object

    又是一篇关于姿势的文章,为什么是”又”呢?因为上个星期刚写完一篇关于Apache Commons Pool的正确使用姿势的文章,点击此处阅读. Redis为我们提供便利的同时,我们也要善待里面的数据 ...

  2. getgrent

    http://baike.baidu.com/link?url=JNyoNvukL-LP7ayYlNNWLv2gPOzn-bjiwuX1CE_QwUTyrRGCWu4NhDW-JznHQoG4aIfw ...

  3. 全国计算机等级考试二级教程-C语言程序设计_第2章_C程序设计的初步知识

    正负号与被除数一致. 3 % (-5) == 3 (-3) % 5 == -3 不用求余运算符,求出余数. int x, y; 答:x - x / y * y; const int i = 10; c ...

  4. react-native 自己搭建热更新服务器

    使用到的框架是 react-native-update react-native-update-cli 这个应该执行热更新的时候的终端命令. 通过这个,自己搭建一个热更新的服务器.

  5. UML的基本关联

     First, a dependency is a semantic relationship between two model elements in which a change to on ...

  6. 模块化利器:RequireJS常用知识

    1. 模块化 目前常见的模块化开发方式,全局空间方式是最基本的一种,另外常见的还有遵循AMD规范的开发方式,遵循CMD规范的开发方式,和ECMAScript 6的开发方式.需要说明的是,CMD和ES6 ...

  7. RHEL5.8安装Oracle11g

    1.安装环境[root@rusky-oracle11g ~]# uname -r2.6.18-308.el5[root@rusky-oracle11g ~]# cat /etc/issueRed Ha ...

  8. OC——封装(初级与高级)

    所谓的封装,就是通过定义方法或者函数去操作成员属性或者成员变量,而不是直接通过指针方式去操作.借此达到提高代码安全性,代码可行性以及代码执行效率的目的. 1:初级封装,对成员变量进行封装. #impo ...

  9. poj2752Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  10. Spring-----自定义属性编辑器

    转载自:http://blog.csdn.net/hekewangzi/article/details/51712963