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ą tak po zastosowaniu jawnie:
<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 w stanie atrybutów, ale można stosować tylko raz do każdej jednostki. Jest dziedziczony przez klasy pochodne po zastosowaniu 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 pojedynczej jednostki, 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 wartość true
. Oba formaty wyświetlane do stosowania wielu atrybutów są prawidłowe.
Jeśli Inherited
jest ustawiona false
wartość , atrybut nie jest dziedziczony przez klasy, które pochodzą z klasy, która jest przypisywana. Na 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 jest stosowany do DClass
za pośrednictwem dziedziczenia.
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 Emocje ion (Visual Basic).
Przykład
W poniższym przykładzie pokazano efekt Inherited
argumentów i AllowMultiple
atrybutu AttributeUsage
oraz sposób wyliczania atrybutów niestandardowych zastosowanych 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