设计模式体现的是一种思想,而思想是指导行为的一切------------开发智能要比机械性的生产商品要简单的多./我要从南走到北,从1到2^10/-----------------Life is a pure flame, and we live by an invisible within us.
15.Attribute 类[摘自MSDN]
上一篇 /
下一篇 2008-05-05 22:07:16
/ 个人分类:::NET Framework 类库::
命名空间: System
-v:Y4Lm
dKfm"Q;_ RqS0程序集: mscorlib(在 mscorlib.dll 中)

语法
<SerializableAttribute> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.All, Inherited :=True, AllowMultiple :=False)> _PublicMustInheritClassAttribute _Implements_Attribute
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets.All, Inherited =true, AllowMultiple =false)]publicabstractclassAttribute : _Attribute
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType::None)]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets::All, Inherited =true, AllowMultiple =false)]publicrefclassAttribute abstract : _Attribute
/** @attribute SerializableAttribute *//** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) *//** @attribute ComVisibleAttribute(true) *//** @attribute AttributeUsageAttribute(AttributeTargets.All, Inherited =true, AllowMultiple =false) */publicabstractclassAttribute implements _Attribute
publicabstractclassAttribute implements _Attribute

备注
Attribute类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性 (Property)、返回值、结构或其他属性 (Attribute)。
属性所提供的信息也称为元数据。元数据可由应用程序在运行时进行检查以控制程序处理数据的方式,也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式。例如,.NET Framework 预定义属性类型并使用属性类型控制运行时行为,某些编程语言使用属性类型表示 .NET Framework 公共类型系统不直接支持的语言功能。
所有属性类型都直接或间接地从Attribute类派生。属性可应用于任何目标元素;多个属性可应用于同一目标元素;并且属性可由从目标元素派生的元素继承。使用AttributeTargets类可以指定属性所应用到的目标元素。
Attribute类提供检索和测试自定义属性的简便方法。有关使用属性的更多信息,请参见利用属性扩展元数据。

示例
下面的代码示例演示Attribute的用法。
C#
usingSystem;
| d{'d&T Z.z0usingSystem.Reflection;
ITPUB个人空间q(x!}s#A{#qITPUB个人空间.T/nU1sT$L5o'KnamespaceCustomAttrCS {
o/WL{$_0 // An enumeration of animals. Start at 1 (0 = uninitialized).ITPUB个人空间Fzf|#g+X2Anwa8@} publicenum Animal {
h_1M`,FZK0 // Pets.ITPUB个人空间4sUH;l8K Dog = 1,
&E'Ab7Uykb@.| C0 Cat,
O$dTe8U ]D7M"Q0 Bird,
{(JYCHXL7x%m}^0 }
slaG/hKZ1c0ITPUB个人空间*H2xT#]8P7kr // A custom attribute to allow a target to have a pet.3b c'~
u2qrz}.| N0 publicclassAnimalTypeAttribute : Attribute {
ITPUB个人空间tS+@P%_4t3lk // The constructor is called when the attribute is set.*W
Z)a[9mN4t0 publicAnimalTypeAttribute(Animal pet) {
dDvieb0 thePet = pet;
c!K-]_\&[eY0 }
ITPUB个人空间d*k{h*qpkITPUB个人空间+D~:T[8g/w]`z7]jZ // Keep a variable internally ...ITPUB个人空间QVu]xOE protectedAnimal thePet;
ITPUB个人空间|GRJ1H#_"LWYITPUB个人空间Z)BKn,Groro3G,D // .. and show a copy to the outside world.g0N%dF| c3|0 publicAnimal Pet {
ITPUB个人空间y#kE
sCY#JrYA get{
returnthePet; }
ITPUB个人空间be'u@z"{RDw5K set{ thePet = Pet; }
$b0z6c&a.I*wD_0 }
k;|DC\-|0 }
2w,n7f)\7|/x/~
N09jQ$j.oX0 // A test class where each method has its own pet.2@"|mc(M0 classAnimalTypeTestClass {
Ia RXeah0 [AnimalType(Animal.Dog)]
ITPUB个人空间}t/`e ?!Zd&p publicvoidDogMethod() {}
Zs4oN\*c)?q0
~,b5l,L
JMfr!q0 [AnimalType(Animal.Cat)]
ITPUB个人空间
Th,`#y6RA publicvoidCatMethod() {}
:H(OG+T-Y%b0Q k0ITPUB个人空间4UVZG,M\,y [AnimalType(Animal.Bird)]
ITPUB个人空间v"c
Z1mk publicvoidBirdMethod() {}
K%tt$p0YJ?0N0 }
8{0LBIjj c0#i q7IYpA0 classDemoClass {
Om a\@L
O-}0 staticvoidMain(
string[] args) {
ITPUB个人空间5l
P~3?q(L2Nk@Q5_ AnimalTypeTestClass testClass =
newAnimalTypeTestClass();
Type type = testClass.GetType();
// Iterate through all the methods of the class..^Ol
iT cmN)}0 foreach(MethodInfo mInfo
intype.GetMethods()) {
W T%]P ?(n.R'VE0 // Iterate through all the Attributes for each method.ITPUB个人空间 T&D"I#^:L+]
Ry"z8b foreach(Attribute attr
inAttribute.GetCustomAttributes(mInfo)) {
// Check for the AnimalType attribute.e r8} N4k;u*lY"Z E0 if(attr.GetType() == typeof(AnimalTypeAttribute))
hC@/L#|!e&b0 Console.WriteLine(
ITPUB个人空间X)@r0f0TBh8T "Method {0} has a pet {1} attribute.",
ITPUB个人空间WgF2mxA&L mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
ITPUB个人空间,O!nnOMU0M }
:fp{q:V"b0#jIVF~*Hv
q0 }
0diUn-c
Qz#nj{G0 }
ITPUB个人空间|5r'h@ vX }
ITPUB个人空间'sP.o
r~_dYef9U}
a
k|[]*P Q0O0^0ITPUB个人空间H]s8tSF/*
-@y
v]wM?.r0* Output:ITPUB个人空间a.{\/Eq4uM
* Method DogMethod has a pet Dog attribute.ITPUB个人空间/t
r'@#z"o,sK
* Method CatMethod has a pet Cat attribute.ITPUB个人空间5s
]$\D
V'Q
* Method BirdMethod has a pet Bird attribute.ITPUB个人空间
?3\w'lIi
|1[i
*/6CDH^+xK$K0
导入论坛
引用链接
收藏
分享给好友
推荐到圈子
管理
举报
TAG: