【Attribute 类】
表示自定义属性的基类。
命名空间: System
程序集: mscorlib(在 mscorlib.dll 中)
语法
Visual Basic(声明)
<SerializableAttribute> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.All, Inherited :=True, AllowMultiple :=False)> _PublicMustInheritClassAttribute _Implements_Attribute
Visual Basic(用法)
DiminstanceAsAttribute
C#
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets.All, Inherited =true, AllowMultiple =false)]publicabstractclassAttribute : _Attribute
Visual C++
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType::None)]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets::All, Inherited =true, AllowMultiple =false)]publicrefclassAttribute abstract : _Attribute
J#
/** @attribute SerializableAttribute *//** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) *//** @attribute ComVisibleAttribute(true) *//** @attribute AttributeUsageAttribute(AttributeTargets.All, Inherited =true, AllowMultiple =false) */publicabstractclassAttribute implements _Attribute
JScript
publicabstractclassAttribute implements _Attribute
备注
Attribute类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性 (Property)、返回值、结构或其他属性 (Attribute)。
属性所提供的信息也称为元数据。元数据可由应用程序在运行时进行检查以控制程序处理数据的方式,也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式。例如,.NET Framework 预定义属性类型并使用属性类型控制运行时行为,某些编程语言使用属性类型表示 .NET Framework 公共类型系统不直接支持的语言功能。
所有属性类型都直接或间接地从Attribute类派生。属性可应用于任何目标元素;多个属性可应用于同一目标元素;并且属性可由从目标元素派生的元素继承。使用AttributeTargets类可以指定属性所应用到的目标元素。
Attribute类提供检索和测试自定义属性的简便方法。有关使用属性的更多信息,请参见利用属性扩展元数据。
示例
下面的代码示例演示Attribute的用法。
C#
usingSystem;
jV725190usingSystem.Reflection;
namespaceCustomAttrCS {
H*v-?6d@b
// An enumeration of animals. Start at 1 (0 = uninitialized).
publicenum Animal {
// Pets.
Dog = 1,
l
Cat,D
Bird,
}
// A custom attribute to allow a target to have a pet.
~P.w}mNr3aQ725190 publicclassAnimalTypeAttribute : Attribute {
? qPdz%e725190 // The constructor is called when the attribute is set.
publicAnimalTypeAttribute(Animal pet) {
thePet = pet;
}
H y
{725190 // Keep a variable internally ...
wa.Li1ZR z5u v"u
protectedAnimal thePet;
// .. and show a copy to the outside world.
u u a Gz[6q-n B
publicAnimal Pet {
get{returnthePet; }
set{ thePet = Pet; }
}
}
X[y
Y^Q725190ITPUBS iv,a" z9_/I X
// A test class where each method has its own pet.
classAnimalTypeTestClass {
bJ!v*E3C
[AnimalType(Animal.Dog)]
publicvoidDogMethod() {}
d725190ITPUB
[AnimalType(Animal.Cat)]
publicvoidCatMethod() {}
v Fr3Pf|m*`W725190 [AnimalType(Animal.Bird)]
publicvoidBirdMethod() {}
}
Is725190 classDemoClass {
staticvoidMain(string[] args) {
R.],g725190 AnimalTypeTestClass testClass =newAnimalTypeTestClass(); Type type = testClass.GetType();// Iterate through all the methods of the class.
foreach(MethodInfo mInfointype.GetMethods()) {
// Iterate through all the Attributes for each method.
foreach(Attribute attrinAttribute.GetCustomAttributes(mInfo)) {// Check for the AnimalType attribute.
if(attr.GetType() == typeof(AnimalTypeAttribute))
Console.WriteLine(
"Method {0} has a pet {1} attribute.",
K T7v,Rm){Xcn725190 mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
}
K,V#fQ
J
B#?b_725190 }
}
/*
* Output:
* Method DogMethod has a pet Dog attribute.
* Method CatMethod has a pet Cat attribute.
* Method BirdMethod has a pet Bird attribute.
*/
COPYRIGHT©2008,HTTP://ZEROBUG.CNBLOGS.COM .ALL RIGHTS RESERVED.