共用方式為


AttributeUsage (C# 和 Visual Basic)

決定可以如何使用自訂屬性類別。 AttributeUsage 是可以套用至自訂屬性定義的屬性,該定義可控制如何能夠套用新的屬性。 明確套用的預設設定如下所示:

    <System.AttributeUsage(System.AttributeTargets.All, 
                       AllowMultiple:=False, 
                       Inherited:=True)> 
    Class NewAttribute
        Inherits System.Attribute
    End Class

[System.AttributeUsage(System.AttributeTargets.All,
                   AllowMultiple = false,
                   Inherited = true)]
class NewAttribute : System.Attribute { }

在此範例中,NewAttribute 類別可以套用至任何能使用屬性的程式碼實體 (Entity),不過每個實體只能套用一次。 此類別是在套用至基底類別時由衍生的類別繼承而來。

AllowMultiple 和 Inherited 引數都是選擇項,因此這段程式碼會具有相同效果:

<System.AttributeUsage(System.AttributeTargets.All)> 
Class NewAttribute
    Inherits System.Attribute
End Class
[System.AttributeUsage(System.AttributeTargets.All)]
class NewAttribute : System.Attribute { }

第一個 AttributeUsage 引數必須是 AttributeTargets 列舉型別的一個或多個項目。 您可以使用 OR 運算子將多個目標型別連結在一起,如下所示:

Imports System


...


<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field)> 
Class NewPropertyOrFieldAttribute
    Inherits Attribute
End Class
using System;


...


[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
class NewPropertyOrFieldAttribute : Attribute { }

如果 AllowMultiple 引數設定為 true,則產生的屬性可以對單一實體套用一次以上,如下所示:

Imports System


...


    <AttributeUsage(AttributeTargets.Class, AllowMultiple:=True)> 
    Class MultiUseAttr
        Inherits Attribute
    End Class

    <MultiUseAttr(), MultiUseAttr()> 
    Class Class1
    End Class

using System;


...


[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class MultiUseAttr : Attribute { }

[MultiUseAttr]
[MultiUseAttr]
class Class1 { }

[MultiUseAttr, MultiUseAttr]
class Class2 { }

在此情況中,由於 AllowMultiple 設定為 true,可以重複套用 MultiUseAttr。 這裡示範的兩種格式都可以有效地套用多個屬性。

如果 Inherited 設定為 false,屬性就不會由使用屬性之類別所衍生的類別繼承。 例如:

Imports System


...


<AttributeUsage(AttributeTargets.Class, Inherited:=False)> 
Class Attr1
    Inherits Attribute
End Class

<Attr1()> 
Class BClass

End Class  

Class DClass
    Inherits BClass
End Class
using System;


...


[AttributeUsage(AttributeTargets.Class, Inherited = false)]
class Attr1 : Attribute { }

[Attr1]
class BClass { }

class DClass : BClass { }

在此情況中,Attr1 就不會透過繼承 (Inheritance) 套用至 DClass。

備註

AttributeUsage 屬性是單一使用的屬性,也就是無法對相同的類別套用一次以上。 AttributeUsage 是 AttributeUsageAttribute 的別名 (Alias)。

如需詳細資訊,請參閱使用反映存取屬性 (C# 和 Visual Basic)

範例

下列範例說明 AttributeUsage 屬性之 Inherited 和 AllowMultiple 引數的效果,以及如何能夠列舉套用至類別的自訂屬性。

Imports System


...


    ' Create some custom attributes:
    <AttributeUsage(System.AttributeTargets.Class, Inherited:=False)> 
    Class A1
        Inherits System.Attribute
    End Class

    <AttributeUsage(System.AttributeTargets.Class)> 
    Class A2
        Inherits System.Attribute
    End Class    

    <AttributeUsage(System.AttributeTargets.Class, AllowMultiple:=True)> 
    Class A3
        Inherits System.Attribute
    End Class


    ' Apply custom attributes to classes:
    <A1(), A2()> 
    Class BaseClass

    End Class

    <A3(), A3()> 
    Class DerivedClass
        Inherits BaseClass
    End Class


    Public Class TestAttributeUsage
        Sub Main()
            Dim b As New BaseClass
            Dim d As New DerivedClass
            ' Display custom attributes for each class.
            Console.WriteLine("Attributes on Base Class:")
            Dim attrs() As Object = b.GetType().GetCustomAttributes(True)

            For Each attr In attrs
                Console.WriteLine(attr)
            Next

            Console.WriteLine("Attributes on Derived Class:")
            attrs = d.GetType().GetCustomAttributes(True)
            For Each attr In attrs
                Console.WriteLine(attr)
            Next            
        End Sub
    End Class

using System;


...


// Create some custom attributes:
[AttributeUsage(System.AttributeTargets.Class, Inherited = false)]
class A1 : System.Attribute { }

[AttributeUsage(System.AttributeTargets.Class)]
class A2 : System.Attribute { }

[AttributeUsage(System.AttributeTargets.Class, AllowMultiple = true)]
class A3 : System.Attribute { }

// Apply custom attributes to classes:
[A1, A2]
class BaseClass { }

[A3, A3]
class DerivedClass : BaseClass { }

public class TestAttributeUsage
{
    static void Main()
    {
        BaseClass b = new BaseClass();
        DerivedClass d = new DerivedClass();

        // Display custom attributes for each class.
        Console.WriteLine("Attributes on Base Class:");
        object[] attrs = b.GetType().GetCustomAttributes(true);
        foreach (Attribute attr in attrs)
        {
            Console.WriteLine(attr);
        }

        Console.WriteLine("Attributes on Derived Class:");
        attrs = d.GetType().GetCustomAttributes(true);
        foreach (Attribute attr in attrs)
        {
            Console.WriteLine(attr);
        }
    }
}

範例輸出

Attributes on Base Class:
A1
A2
Attributes on Derived Class:
A3
A3
A2

請參閱

參考

反映 (C# 和 Visual Basic)

屬性 (C# 和 Visual Basic)

建立自訂屬性 (C# 和 Visual Basic)

使用反映存取屬性 (C# 和 Visual Basic)

Attribute

System.Reflection

概念

C# 程式設計手冊

其他資源

Visual Basic 程式設計手冊

使用屬性擴充中繼資料