to 楼上: 我明白了,Attribute是对类及方法等“添加”额外信息,但是咱们什么时候需要“添加”这些信息呢,如何添加呢,再如何通过反射查看呢,不好意思,我实在是不明白。
下面的代码实例说明了 Attribute 的用法: [Visual Basic] Imports System Imports System.ReflectionPublic Module CustomAttrVB ' An enumeration of animals. Start at 1 (0 = uninitialized). Public Enum Animal ' Pets Dog = 1 Cat Bird End Enum ' Visual Basic requires the AttributeUsage be specified. ' A custom attribute to allow a target to have a pet. <AttributeUsage(AttributeTargets.Method)> _ Public Class AnimalTypeAttribute Inherits Attribute ' The constructor is called when the attribute is set. Public Sub New(ByVal animal As Animal) Me.thePet = animal End Sub ' Keep a variable internally ... Protected thePet As Animal ' .. and show a copy to the outside world. Public Property Pet() As Animal Get Return thePet End Get Set(ByVal Value As Animal) thePet = Value End Set End Property End Class ' A test class where each method has its own pet. Class AnimalTypeTestClass <AnimalType(Animal.Dog)> _ Public Sub DogMethod() End Sub <AnimalType(Animal.Cat)> _ Public Sub CatMethod() End Sub <AnimalType(Animal.Bird)> _ Public Sub BirdMethod() End Sub End Class ' The runtime test. Sub Main() Dim testClass As New AnimalTypeTestClass() Dim tcType As Type = testClass.GetType() Dim mInfo As MethodInfo ' Iterate through all the methods of the class. For Each mInfo In tcType.GetMethods() Dim attr As Attribute ' Iterate through all the attributes of the method. For Each attr In Attribute.GetCustomAttributes(mInfo) If TypeOf attr Is AnimalTypeAttribute Then Dim attrCustom As AnimalTypeAttribute = _ CType(attr, AnimalTypeAttribute) Console.WriteLine("Method {0} has a pet {1} attribute.", _ mInfo.Name(), attrCustom.Pet.ToString()) End If Next Next End Sub End Module' Output: ' Method DogMethod has a pet Dog attribute. ' Method CatMethod has a pet Cat attribute. ' Method BirdMethod has a pet Bird attribute. [C#] using System; using System.Reflection;namespace CustomAttrCS { // An enumeration of animals. Start at 1 (0 = uninitialized). public enum Animal { // Pets. Dog = 1, Cat, Bird, } // A custom attribute to allow a target to have a pet. public class AnimalTypeAttribute : Attribute { // The constructor is called when the attribute is set. public AnimalTypeAttribute(Animal pet) { thePet = pet; } // Keep a variable internally ... protected Animal thePet; // .. and show a copy to the outside world. public Animal Pet { get { return thePet; } set { thePet = Pet; } } } // A test class where each method has its own pet. class AnimalTypeTestClass { [AnimalType(Animal.Dog)] public void DogMethod() {} [AnimalType(Animal.Cat)] public void CatMethod() {} [AnimalType(Animal.Bird)] public void BirdMethod() {} } class DemoClass { static void Main(string[] args) { AnimalTypeTestClass testClass = new AnimalTypeTestClass(); Type type = testClass.GetType(); // Iterate through all the methods of the class. foreach(MethodInfo mInfo in type.GetMethods()) { // Iterate through all the Attributes for each method. foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo)) { // Check for the AnimalType attribute. if (attr.GetType() == typeof(AnimalTypeAttribute)) Console.WriteLine( "Method {0} has a pet {1} attribute.", mInfo.Name, ((AnimalTypeAttribute)attr).Pet); } } } } }/* * Output: * Method DogMethod has a pet Dog attribute. * Method CatMethod has a pet Cat attribute. * Method BirdMethod has a pet Bird attribute. */
特性(Attributes)是一种崭新的声明性信息。我们不仅可以通过特性来定义设计层面的信息(例如help file, URL for documentation)以及运行时(run-time)信息(例如使XML与class相联系),而且我们还可以利用特性建立自描述(self-describing)组件。在这篇教程中,我们将会看到如何建立和添加特性到各种程序实体以及如何在运行时环境中获取特性信息。
http://www.enet.com.cn/eschool/inforcenter/A20040407300659.html
1:已定义Attribute,如:DllImport(用于将方法标记为在外部定义),Conditonal(用于将阻止方法被编译,除非满足Conditonal条件)等等.2:定制属性,就是你自己定义的属性,继承自Attribute,如可以定义装配件的版本信息等
我明白了,Attribute是对类及方法等“添加”额外信息,但是咱们什么时候需要“添加”这些信息呢,如何添加呢,再如何通过反射查看呢,不好意思,我实在是不明白。
[Visual Basic]
Imports System
Imports System.ReflectionPublic Module CustomAttrVB ' An enumeration of animals. Start at 1 (0 = uninitialized).
Public Enum Animal
' Pets
Dog = 1
Cat
Bird
End Enum ' Visual Basic requires the AttributeUsage be specified.
' A custom attribute to allow a target to have a pet.
<AttributeUsage(AttributeTargets.Method)> _
Public Class AnimalTypeAttribute
Inherits Attribute ' The constructor is called when the attribute is set.
Public Sub New(ByVal animal As Animal)
Me.thePet = animal
End Sub ' Keep a variable internally ...
Protected thePet As Animal ' .. and show a copy to the outside world.
Public Property Pet() As Animal
Get
Return thePet
End Get
Set(ByVal Value As Animal)
thePet = Value
End Set
End Property End Class ' A test class where each method has its own pet.
Class AnimalTypeTestClass <AnimalType(Animal.Dog)> _
Public Sub DogMethod()
End Sub <AnimalType(Animal.Cat)> _
Public Sub CatMethod()
End Sub <AnimalType(Animal.Bird)> _
Public Sub BirdMethod()
End Sub
End Class ' The runtime test.
Sub Main()
Dim testClass As New AnimalTypeTestClass()
Dim tcType As Type = testClass.GetType()
Dim mInfo As MethodInfo
' Iterate through all the methods of the class.
For Each mInfo In tcType.GetMethods()
Dim attr As Attribute
' Iterate through all the attributes of the method.
For Each attr In Attribute.GetCustomAttributes(mInfo)
If TypeOf attr Is AnimalTypeAttribute Then
Dim attrCustom As AnimalTypeAttribute = _
CType(attr, AnimalTypeAttribute)
Console.WriteLine("Method {0} has a pet {1} attribute.", _
mInfo.Name(), attrCustom.Pet.ToString())
End If
Next
Next
End Sub
End Module' Output:
' Method DogMethod has a pet Dog attribute.
' Method CatMethod has a pet Cat attribute.
' Method BirdMethod has a pet Bird attribute.
[C#]
using System;
using System.Reflection;namespace CustomAttrCS {
// An enumeration of animals. Start at 1 (0 = uninitialized).
public enum Animal {
// Pets.
Dog = 1,
Cat,
Bird,
} // A custom attribute to allow a target to have a pet.
public class AnimalTypeAttribute : Attribute {
// The constructor is called when the attribute is set.
public AnimalTypeAttribute(Animal pet) {
thePet = pet;
} // Keep a variable internally ...
protected Animal thePet; // .. and show a copy to the outside world.
public Animal Pet {
get { return thePet; }
set { thePet = Pet; }
}
} // A test class where each method has its own pet.
class AnimalTypeTestClass {
[AnimalType(Animal.Dog)]
public void DogMethod() {} [AnimalType(Animal.Cat)]
public void CatMethod() {} [AnimalType(Animal.Bird)]
public void BirdMethod() {}
} class DemoClass {
static void Main(string[] args) {
AnimalTypeTestClass testClass = new AnimalTypeTestClass();
Type type = testClass.GetType();
// Iterate through all the methods of the class.
foreach(MethodInfo mInfo in type.GetMethods()) {
// Iterate through all the Attributes for each method.
foreach (Attribute attr in
Attribute.GetCustomAttributes(mInfo)) {
// Check for the AnimalType attribute.
if (attr.GetType() == typeof(AnimalTypeAttribute))
Console.WriteLine(
"Method {0} has a pet {1} attribute.",
mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
} }
}
}
}/*
* Output:
* Method DogMethod has a pet Dog attribute.
* Method CatMethod has a pet Cat attribute.
* Method BirdMethod has a pet Bird attribute.
*/