AttributeUsage (Visual Basic)
Určuje způsob použití vlastní třídy atributů. AttributeUsage
je atribut, který lze použít u definic vlastních atributů k řízení způsobu použití nového atributu. Výchozí nastavení při explicitní použití vypadají takto:
<System.AttributeUsage(System.AttributeTargets.All,
AllowMultiple:=False,
Inherited:=True)>
Class NewAttribute
Inherits System.Attribute
End Class
V tomto příkladu lze třídu použít pro libovolnou entitu NewAttribute
kódu, která je schopna atributu, ale lze ji použít pouze jednou pro každou entitu. Dědí se odvozenými třídami při použití na základní třídu.
Argumenty AllowMultiple
a Inherited
argumenty jsou volitelné, takže tento kód má stejný účinek:
<System.AttributeUsage(System.AttributeTargets.All)>
Class NewAttribute
Inherits System.Attribute
End Class
První AttributeUsage
argument musí být jeden nebo více prvků výčtu AttributeTargets . S operátorem OR je možné propojit více cílových typů, například takto:
<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field)>
Class NewPropertyOrFieldAttribute
Inherits Attribute
End Class
AllowMultiple
Pokud je argument nastaven na true
, výsledný atribut lze použít více než jednou pro jednu entitu, například takto:
<AttributeUsage(AttributeTargets.Class, AllowMultiple:=True)>
Class MultiUseAttr
Inherits Attribute
End Class
<MultiUseAttr(), MultiUseAttr()>
Class Class1
End Class
V tomto případě MultiUseAttr
lze opakovaně použít, protože AllowMultiple
je nastavena na true
. Oba formáty zobrazené pro použití více atributů jsou platné.
Pokud Inherited
je nastavena na false
, pak atribut není zděděna třídami odvozenými z třídy, která je přiřazena. Příklad:
<AttributeUsage(AttributeTargets.Class, Inherited:=False)>
Class Attr1
Inherits Attribute
End Class
<Attr1()>
Class BClass
End Class
Class DClass
Inherits BClass
End Class
V tomto případě Attr1
se nepoužije na DClass
dědičnost.
Poznámky
Atribut AttributeUsage
je atribut s jedním použitím– nelze jej použít více než jednou pro stejnou třídu. AttributeUsage
je alias pro AttributeUsageAttribute.
Další informace naleznete v tématu Přístup k atributům pomocí Reflexe ion (Visual Basic).
Příklad
Následující příklad ukazuje účinek Inherited
a AllowMultiple
argumenty atributu AttributeUsage
a způsob použití vlastních atributů na třídu lze vytvořit výčet.
' 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
Ukázkový výstup
Attributes on Base Class:
A1
A2
Attributes on Derived Class:
A3
A3
A2