如果沒有檢索自訂屬性的資訊和對其進行操作的方法,則定義自訂屬性並將其放置在原始碼中就沒有意義。C# 具有一個反射系統,可用來檢索用自訂屬性定義的資訊。主要方法是 GetCustomAttributes,它返回對象數組,這些對象在運行時等效於原始碼屬性。此方法具有多個重載版本。
屬性規範,如:
Code
[Author("H. Ackerman", version = 1.1)]
class SampleClass
在概念上等效於:
Code
Author anonymousAuthorObject = new Author("H. Ackerman");
anonymousAuthorObject.version = 1.1;
但是,直到查詢 SampleClass 以擷取屬性時才會執行此代碼。對 SampleClass 調用 GetCustomAttributes 會導致按上述方式構造並初始化一個 Author 對象。如果類還有其他屬性,則其他屬性對象的以類似方式構造。然後 GetCustomAttributes 返回 Author 對象和數組中的任何其他屬性對象。之後就可以對此數組進行迭代,確定根據每個數組元素的類型所應用的屬性,並從屬性對象中提取資訊。
樣本
下面是一個完整的樣本。定義一個自訂屬性,將其應用於若干實體並通過反射進行檢索。
Code
[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;
version = 1.0; // Default value
}
public string GetName()
{
return name;
}
}
[Author("H. Ackerman")]
private class FirstClass
{
//
}
// No Author attribute
private class SecondClass
{
//
}
[Author("H. Ackerman"), Author("M. Knott", version = 2.0)]
private class ThirdClass
{
//
}
class TestAuthorAttribute
{
static void Main()
{
PrintAuthorInfo(typeof(FirstClass));
PrintAuthorInfo(typeof(SecondClass));
PrintAuthorInfo(typeof(ThirdClass));
}
private static void PrintAuthorInfo(System.Type t)
{
System.Console.WriteLine("Author information for {0}", t);
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // reflection
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);
}
}
}
}
輸出:
Author information for FirstClass
H. Ackerman, version 1.00
Author information for SecondClass
Author information for ThirdClass
H. Ackerman, version 1.00
M. Knott, version 2.00