会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
gxc
永远不要认为有什么事情是理所当然的!
::
首页
::
博问
::
闪存
::
新随笔
::
联系
::
订阅
::
管理
::
公告
标签的使用(2)
using
System;
using
System.Reflection;
namespace
CustomAttrCS
{
public
enum
Animal
{Dog
=
1
,Cat,Bird,Pig}
[AttributeUsage(AttributeTargets.Class,Inherited
=
false
,AllowMultiple
=
true
)]
public
class
AnimalTypeAttribute : Attribute
{
protected
Animal thePet;
public
Animal Pet
{
get
{
return
thePet; }
set
{ thePet
=
Pet; }
}
public
AnimalTypeAttribute(Animal pet)
{
thePet
=
pet;
}
}
[AnimalType(Animal.Pig)]
[AnimalType(Animal.Dog)]
class
AnimalTypeTestClass
{
}
class
DemoClass
{
static
void
Main(
string
[] args)
{
AnimalTypeTestClass testClass
=
new
AnimalTypeTestClass();
Type type
=
testClass.GetType();
//
获取type所拥有的标签.type.GetCustomAttributes(typeof(AnimalTypeAttribute)
AnimalTypeAttribute[] attr
=
(AnimalTypeAttribute[])type.GetCustomAttributes(
typeof
(AnimalTypeAttribute),
false
);
for
(
int
i
=
0
; i
<
attr.Length; i
++
)
{
Console.WriteLine(attr[i].Pet);
}
Console.Read();
}
}
}
posted on
2006-03-29 15:08
gxc
阅读(
184
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部