潇潇洒洒

 

得到枚举的自定义描述信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace EnumAttributeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetEnumDescirbeString<TestEnum>(TestEnum.SecondType));
        }

        public static string GetEnumDescirbeString<T>(T I_EnumValue)
        {
            return ((DescribeAttribute)typeof(T).GetField(I_EnumValue.ToString()).GetCustomAttributes(typeof(DescribeAttribute), false)[0]).Describe;
        }
    }

    public enum TestEnum
    {
        [Describe("aaa")]
        FirstType,

        [Describe("bbb")]
        SecondType,

        [Describe("ccc")]
        ThirdType,
    }

    public class DescribeAttribute : Attribute
    {
        private string m_describe;

        public string Describe
        {
            get { return m_describe; }
            set { m_describe = value; }
        }

        public DescribeAttribute(string describe)
        {
            m_describe = describe;
        }
    }
}

posted on 2011-02-11 00:05  肖磊  阅读(208)  评论(0编辑  收藏  举报

导航