共用方式為


使用反射存取屬性

如果您可以定義自定義屬性並將它們放入原始程式碼,但沒有辦法擷取那些資訊並對其採取行動,這樣的事實就沒有太大的價值。 藉由使用反映,您可以擷取以自定義屬性定義的資訊。 關鍵方法是 GetCustomAttributes,它會傳回物件的陣列,這些物件在執行時等效於原始碼屬性。 此方法有許多重載版本。 如需詳細資訊,請參閱Attribute

屬性規格,例如:

[Author("P. Ackerman", Version = 1.1)]
class SampleClass { }

在概念上相當於下列程序代碼:

var anonymousAuthorObject = new Author("P. Ackerman")
{
    Version = 1.1
};

不過,程式碼不會執行,除非查詢 SampleClass 的屬性。 呼叫 GetCustomAttributesSampleClass時,會導致建構並初始化Author物件。 如果類別具有其他屬性,則會以類似的方式建構其他屬性物件。 GetCustomAttributes 接著會 Author 傳回 物件和陣列中的任何其他屬性物件。 然後,您可以逐一查看此陣列、根據每個陣列元素的類型來判斷套用的屬性,以及從屬性物件擷取資訊。

以下是完整的範例。 自訂屬性已定義,套用至數個實體,並透過反射擷取。

// Multiuse attribute.
[System.AttributeUsage(System.AttributeTargets.Class |
                       System.AttributeTargets.Struct,
                       AllowMultiple = true)  // Multiuse attribute.
]
public class AuthorAttribute : System.Attribute
{
    string Name;
    public string Version;

    public AuthorAttribute(string name)
    {
        Name = name;

        // Default value.
        Version = "1.0";
    }

    public string GetName() => 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
{
    public 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 {t}");

        // Using reflection.
        System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  // Reflection.

        // Displaying output.
        foreach (System.Attribute attr in attrs)
        {
            if (attr is AuthorAttribute a)
            {
                System.Console.WriteLine($"   {a.GetName()}, version {a.Version}");
            }
        }
    }
}
/* Output:
    Author information for FirstClass
       P. Ackerman, version 1.0
    Author information for SecondClass
    Author information for ThirdClass
       R. Koch, version 2.0
       P. Ackerman, version 1.0
*/

另請參閱