首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么获取自定义枚举类型中的中文注释

2011-12-21 
如何获取自定义枚举类型中的中文注释?如题[解决办法]楼主可以参考如下的代码:using Systemusing System.R

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

[解决办法]
楼主可以参考如下的代码:

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

}
}
}
}

热点排行