Partager via


AttributeUsage (C# Programming Guide) 

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 went applied explicitly:

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

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 : System.Attribute { }

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

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

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

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

[MultiUseAttr]
[MultiUseAttr]
class Class1 { }

[MultiUseAttr, MultiUseAttr]
class Class2 { }

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:

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

[Attr1]
class BClass { }

class DClass : BClass { }

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

Remarks

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 With Reflection (C# Programming Guide).

Example

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.

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);
        }
    }
}

Sample Output

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

See Also

Reference

Using Attributes (C# Programming Guide)
Disambiguating Attribute Targets (C# Programming Guide)
Creating Custom Attributes (C# Programming Guide)
Accessing Attributes With Reflection (C# Programming Guide)
Attribute
System.Reflection

Concepts

C# Programming Guide
Reflection (C# Programming Guide)
Attributes (C# Programming Guide)