日期:2014-05-20  浏览次数:20819 次

如何获取自定义枚举类型中的中文注释?
如题

------解决方案--------------------
楼主可以参考如下的代码:

using System;
using System.Reflection;
using System.ComponentModel;

namespace CustomAttrCS
{
enum test
{
[Description( "我的一个测试 ")]
one,
[Description( "我的第二个测试 ")]
two,
[Description( "我的第三个测试 ")]
three
}

class DemoClass
{
static void Main(string[] args)
{
Type type = typeof(test);
foreach (MemberInfo mInfo in type.GetMembers())
{
foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
{
if (attr.GetType() == typeof(DescriptionAttribute))
{
Console.WriteLine(((DescriptionAttribute)attr).Description);
}
}

}
}
}
}