AttributeUsage (C# and Visual Basic)

Determines how a custom attribute class can be used. AttributeUsage is an attribute that can be applied to custom attribute definitions to control how the new attribute can be applied. The default settings look like this when applied explicitly:

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

In this example, the NewAttribute class can be applied to any attribute-able code entity, but can be applied only once to each entity. It is inherited by derived classes when applied to a base class.

The AllowMultiple and Inherited arguments are optional, so this code has the same effect:

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

The first AttributeUsage argument must be one or more elements of the AttributeTargets enumeration. Multiple target types can be linked together with the OR operator, like this:

Imports System


...


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

If the AllowMultiple argument is set to true, then the resulting attribute can be applied more than once to a single entity, like this:

Imports System


...


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

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

In this case MultiUseAttr can be applied repeatedly because AllowMultiple is set to true. Both formats shown for applying multiple attributes are valid.

If Inherited is set to false, then the attribute is not inherited by classes that are derived from a class that is attributed. For example:

Imports System


...


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

<Attr1()> 
Class BClass

End Class   

Class DClass
    Inherits BClass
End Class

In this case Attr1 is not applied to DClass via inheritance.

The AttributeUsage attribute is a single-use attribute--it cannot be applied more than once to the same class. AttributeUsage is an alias for AttributeUsageAttribute.

For more information, see Accessing Attributes by Using Reflection (C# and Visual Basic).

The following example demonstrates the effect of the Inherited and AllowMultiple arguments to the AttributeUsage attribute, and how the custom attributes applied to a class can be enumerated.

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

Attributes on Base Class:
A1
A2
Attributes on Derived Class:
A3
A3
A2
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft