Udostępnij za pośrednictwem


AttributeUsage (Visual Basic)

Określa, jak można użyć niestandardowej klasy atrybutów. AttributeUsage to atrybut, który można zastosować do niestandardowych definicji atrybutów w celu kontrolowania sposobu stosowania nowego atrybutu. Ustawienia domyślne wyglądają następująco, gdy zostaną zastosowane w sposób jawny:

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

W tym przykładzie klasę NewAttribute można zastosować do dowolnej jednostki kodu, którą można przypisać atrybutami, ale można ją przypisać tylko raz do każdej jednostki. Jest dziedziczony przez klasy pochodne w przypadku zastosowania do klasy bazowej.

Argumenty AllowMultiple i Inherited są opcjonalne, więc ten kod ma taki sam efekt:

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

Pierwszy AttributeUsage argument musi być co najmniej jednym elementem AttributeTargets wyliczenia. Wiele typów docelowych może być połączonych z operatorem OR, w następujący sposób:

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

AllowMultiple Jeśli argument jest ustawiony na true wartość, atrybut wynikowy można zastosować więcej niż raz do pojedynczego obiektu w następujący sposób:

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

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

W takim przypadku MultiUseAttr można wielokrotnie stosować, ponieważ AllowMultiple jest ustawiona na true. Oba formaty wyświetlane do stosowania wielu atrybutów są prawidłowe.

Jeśli Inherited jest ustawiona na false, atrybut nie jest dziedziczony przez klasy, które pochodzą z klasy, która jest oznaczona atrybutem. Przykład:

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

<Attr1()>
Class BClass

End Class

Class DClass
    Inherits BClass
End Class

W tym przypadku Attr1 nie ma zastosowania do DClass przez dziedziczenie.

Uwagi

Atrybut AttributeUsage jest atrybutem pojedynczego użycia — nie można go zastosować więcej niż raz do tej samej klasy. AttributeUsage to alias dla elementu AttributeUsageAttribute.

Aby uzyskać więcej informacji, zobacz Uzyskiwanie dostępu do atrybutów przy użyciu odbicia (Visual Basic).

Przykład

W poniższym przykładzie zaprezentowano wpływ argumentów Inherited i AllowMultiple na atrybut AttributeUsage oraz sposób, w jaki można enumerować atrybuty niestandardowe zastosowane do klasy.

' 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

Przykładowe dane wyjściowe

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

Zobacz także