public sealed class AttributeUsageAttribute : Attribute
Object
Attribute
AttributeUsageAttribute
mscorlib
BCL
Specifies the behavior of a custom attribute when that attribute is defined.
[Note: Custom attributes can 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 can be associated with; whether the attribute can or cannot be inherited by derived elements; and whether multiple instances of the attribute can or cannot be allowed on the same target element. ]
AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple=false, Inherited=true)
System Namespace
AttributeUsageAttribute Constructors
AttributeUsageAttribute Constructor
AttributeUsageAttribute Properties
AttributeUsageAttribute.AllowMultiple Property
AttributeUsageAttribute.Inherited Property
AttributeUsageAttribute.ValidOn Property
public AttributeUsageAttribute(AttributeTargets validOn);
Constructs and initializes a new instance of the AttributeUsageAttribute class.
- 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.
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
andtrue
respectively).
System.AttributeUsageAttribute Class, System Namespace
public bool AllowMultiple { get; set; }
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.
A Boolean wheretrue
indicates more than one instance of the attribute is permitted to be applied; otherwise,false
. The default isfalse
.
[Note: It is expected that compilers will validate this property; this property is not validated during execution.]
Example #1:
The following example demonstrates the use of System.AttributeUsageAttribute.AllowMultiple. If
AllowMultiple
for an attribute is set totrue
, more than one of those attributes can 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 tofalse
.
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
System.AttributeUsageAttribute Class, System Namespace
public bool Inherited { get; set; }
Gets or sets a Boolean value indicating whether the attribute can be inherited by subclasses of the class to which the attribute is applied.
true
indicates the attribute is inherited by subclasses; otherwise,false
. The default istrue
.
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.
System.AttributeUsageAttribute Class, System Namespace
public AttributeTargets ValidOn { get; }
Gets the set of values sent to the AttributeUsageAttribute constructor that indicate to which targets the custom attribute can be applied.
One or more of the AttributeTargets values sent to the constructor, combined by a bitwise OR operation.
This property is read-only.
System.AttributeUsageAttribute Class, System Namespace