使用反映存取屬性 (C# 和 Visual Basic)
如果沒有可以擷取資訊並加以執行的方法,那麼定義自訂屬性並且將他們放置在您的原始程式碼中實際上的價值不大。透過使用反映,您可以擷取由自訂屬性定義的資訊。關鍵的方法是 GetCustomAttributes,它能傳回一個物件陣列,這是原始程式碼屬性在執行階段的對等用法。這個方法擁有數種多載版本。如需詳細資訊,請參閱Attribute。
屬性規格例如:
<Author("P. Ackerman", Version:=1.1)>
Class SampleClass
' P. Ackerman's code goes here...
End Class
[Author("P. Ackerman", version = 1.1)]
class SampleClass
在概念上相等於:
Dim anonymousAuthorObject As Author = New Author("P. Ackerman")
anonymousAuthorObject.version = 1.1
Author anonymousAuthorObject = new Author("P. Ackerman");
anonymousAuthorObject.version = 1.1;
但是,除非對 SampleClass 查詢屬性,否則不會執行程式碼。在 SampleClass 上呼叫 GetCustomAttributes,會建構並初始化 Author 物件,如上所述。如果此類別具有其他屬性,便會以類似方式建構其他屬性物件。然後,GetCustomAttributes 會以陣列傳回 Author 物件和其他任何屬性物件。然後,您可以逐一查看陣列,以每項陣列元素的類型為基礎來決定所要套用的屬性,並且從屬性物件擷取資訊。
範例
以下是一個完整的範例,其中定義了一個自訂屬性,套用至數個實體,並且經由反映來擷取。
' Multiuse attribute
<System.AttributeUsage(System.AttributeTargets.Class Or
System.AttributeTargets.Struct,
AllowMultiple:=True)>
Public Class Author
Inherits System.Attribute
Private name As String
Public version As Double
Sub New(ByVal authorName As String)
name = authorName
' Default value
version = 1.0
End Sub
Function GetName() As String
Return name
End Function
End Class
' Class with the Author attribute
<Author("P. Ackerman")>
Public Class FirstClass
End Class
' Class without the Author attribute
Public Class SecondClass
End Class
' Class with multiple Author attributes.
<Author("P. Ackerman"), Author("R. Koch", Version:=2.0)>
Public Class ThirdClass
End Class
Class TestAuthorAttribute
Sub Main()
PrintAuthorInfo(GetType(FirstClass))
PrintAuthorInfo(GetType(SecondClass))
PrintAuthorInfo(GetType(ThirdClass))
End Sub
Private Shared Sub PrintAuthorInfo(ByVal t As System.Type)
System.Console.WriteLine("Author information for {0}", t)
' Using reflection
Dim attrs() As System.Attribute = System.Attribute.GetCustomAttributes(t)
' Displaying output
For Each attr In attrs
Dim a As Author = CType(attr, Author)
System.Console.WriteLine(" {0}, version {1:f}", a.GetName(), a.version)
Next
End Sub
' Output:
' Author information for FirstClass
' P. Ackerman, version 1.00
' Author information for SecondClass
' Author information for ThirdClass
' R. Koch, version 2.00
' P. Ackerman, version 1.00
End Class
// Multiuse attribute.
[System.AttributeUsage(System.AttributeTargets.Class |
System.AttributeTargets.Struct,
AllowMultiple = true) // Multiuse attribute.
]
public class Author : System.Attribute
{
string name;
public double version;
public Author(string name)
{
this.name = name;
// Default value.
version = 1.0;
}
public string GetName()
{
return name;
}
}
// Class with the Author attribute.
[Author("P. Ackerman")]
public class FirstClass
{
// ...
}
// Class without the Author attribute.
public class SecondClass
{
// ...
}
// Class with multiple Author attributes.
[Author("P. Ackerman"), Author("R. Koch", version = 2.0)]
public class ThirdClass
{
// ...
}
class TestAuthorAttribute
{
static void Test()
{
PrintAuthorInfo(typeof(FirstClass));
PrintAuthorInfo(typeof(SecondClass));
PrintAuthorInfo(typeof(ThirdClass));
}
private static void PrintAuthorInfo(System.Type t)
{
System.Console.WriteLine("Author information for {0}", t);
// Using reflection.
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // Reflection.
// Displaying output.
foreach (System.Attribute attr in attrs)
{
if (attr is Author)
{
Author a = (Author)attr;
System.Console.WriteLine(" {0}, version {1:f}", a.GetName(), a.version);
}
}
}
}
/* Output:
Author information for FirstClass
P. Ackerman, version 1.00
Author information for SecondClass
Author information for ThirdClass
R. Koch, version 2.00
P. Ackerman, version 1.00
*/