System.AttributeUsageAttribute Class

public sealed class AttributeUsageAttribute : Attribute

Base Types

Object
  Attribute
    AttributeUsageAttribute

Assembly

mscorlib

Library

BCL

Summary

Specifies the behavior of a custom attribute when that attribute is defined.

Description

[Note: Custom attributes may be applied to various application ("target") elements, such as classes, parameters, and structures (see AttributeTargets for the full list). The AttributeUsageAttribute class contains three properties that govern custom attribute behavior: the kinds of application elements the attribute may be associated with; whether the attribute may or may not be inherited by derived elements; and whether multiple instances of the attribute may or may not be allowed on the same target element. ]

Attributes

AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple=false, Inherited=true)

See Also

System Namespace

Members

AttributeUsageAttribute Constructors

AttributeUsageAttribute Constructor

AttributeUsageAttribute Properties

AttributeUsageAttribute.AllowMultiple Property
AttributeUsageAttribute.Inherited Property
AttributeUsageAttribute.ValidOn Property


AttributeUsageAttribute Constructor

public AttributeUsageAttribute(AttributeTargets validOn);

Summary

Constructs and initializes a new instance of the AttributeUsageAttribute class.

Parameters

validOn
The set of application elements to which the attribute will be applied. When indicating multiple application elements, validOn is a bitwise OR combination of AttributeTargets enumeration values.

Description

The new instance will be constructed with the specified value of validOn and the properties System.AttributeUsageAttribute.AllowMultiple and System.AttributeUsageAttribute.Inherited set to their default values (false and true respectively).

See Also

System.AttributeUsageAttribute Class, System Namespace

AttributeUsageAttribute.AllowMultiple Property

public bool AllowMultiple { get; set; }

Summary

Gets or sets a value indicating whether more than one instance of a specified attribute is permitted to be applied to any given program element.

Property Value

A Boolean where true indicates more than one instance of the attribute is permitted to be applied; otherwise, false . The default is false .

Description

[Note: It is expected that compilers will validate this property; this property is not validated during execution.]

Example

Example #1:

The following example demonstrates the use of System.AttributeUsageAttribute.AllowMultiple. If AllowMultiple for an attribute is set to true , more than one of those attributes may be assigned to any given program element.

using System;

[AttributeUsageAttribute( AttributeTargets.Class |
                          AttributeTargets.Struct,
                          AllowMultiple = true )]
public class Author : Attribute {

  public Author(string name) { this.name = name; }
  public string name;
}

[Author( "John Doe" )]
[Author( "John Q Public" )]
class JohnsClass {

  public static void Main() {}
}
Example #2:

The following example demonstrates an error that is expected to be caught by compilers: the sample attempts to assign multiple instances of an attribute for which AllowMultiple was set to false .

using System;

[AttributeUsageAttribute( AttributeTargets.Class |
                          AttributeTargets.Struct,
                          AllowMultiple = false )]
public class Author : Attribute {

  public Author(string name) { this.name = name; }
  public string name;
}

[Author( "John Doe" )]
[Author( "John Q Public" )]
class JohnsClass {

  public static void Main() {}
}
This should throw an error similar to:

error CS0579: Duplicate 'Author' attribute

See Also

System.AttributeUsageAttribute Class, System Namespace

AttributeUsageAttribute.Inherited Property

public bool Inherited { get; set; }

Summary

Gets or sets a Boolean value indicating whether the attribute can be inherited by subclasses of the class to which the attribute is applied.

Property Value

true indicates the attribute is inherited by subclasses; otherwise, false . The default is true .

Description

Information on an inherited attribute will be included in the metadata for the class on which it is applied, but will not be included in the metadata for classes that derive from it. A metadata consumer (such as reflection) is required therefore to traverse up the inheritance chain of a class if that consumer is interested in Attribute data that is marked inherited, but applied to an ancestor class. There is nothing for the compiler to validate at compile time.

See Also

System.AttributeUsageAttribute Class, System Namespace

AttributeUsageAttribute.ValidOn Property

public AttributeTargets ValidOn { get; }

Summary

Gets the set of values sent to the AttributeUsageAttribute constructor that indicate to which targets the custom attribute may be applied.

Property Value

One or more of the AttributeTargets values sent to the constructor, combined by a bitwise OR operation.

Description

This property is read-only.

See Also

System.AttributeUsageAttribute Class, System Namespace