获取枚举Name,Value,Description两种方法

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;
    }

}

上一篇:为什么我不再用 .NET 框架


下一篇:java学习笔记11--集合总结