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. android透明度和css透明度

    值越小,越透明 css:0.1-------1 android:#00-----------#ff

  2. NFC-P2P MODE

    今日看见有关国内电信业者要合组TSM (Trusted Service Manager)提供NFC 服务的新闻, 这是属于NFC 所能提供的3种Mode中的Card emulation mode (就 ...

  3. html5 新增语义标签

    一份模板: <body> <header> <hgroup> <h1>Page title</h1> <h2>Page subt ...

  4. js的数组操作

    用 js有很久了,但都没有深究过js的数组形式.偶尔用用也就是简单的string.split(char).这段时间做的一个项目,用到数组的地方很多,自以为js高手的自己居然无从下手,一下狠心,我学!呵 ...

  5. python图片小爬虫

    import re import urllib import os def rename(name): name = name + '.jpg' return name def getHtml(url ...

  6. Android EditText限制输入一些固定字符的属性

    android:digits="abcdefghijklmnopqrstuvwxyz1234567890" 仅仅能输入这些

  7. 多进程用户并发处理Demo(C#版)

    这个示例主要演示的是在多进程操作数据库时,如何避免并发重复数据入库的例子. 过多的线程理论不再阐述,网上.书上皆有. 项目采用 Asp.Net Framework 4.5 / Mysql 5.4 数据 ...

  8. CSS备忘-1

    CSS 可以通过以下方式添加到HTML中: 内联样式- 在HTML元素中使用"style" 属性 内部样式表 -在HTML文档头部 <head> 区域使用<sty ...

  9. 图片占位 css

    手机端图片高度和宽度不能自动比例缩小的问题 <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...

  10. 安卓开发之RecyclerView

    RecyclerView是一个非常好用的控件,它的效果和ListView很相似,甚至可以说RecyclerView的出现是来取代ListView的 RecyclerView比ListView更加灵活, ...