Namespace: System
AttributeUsageAttribute Class
Applies a user-defined attribute to an assembly, type, method, property, event, or parameter.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
[AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = false)]
public sealed class AttributeUsageAttribute : Attribute
Remarks
The AttributeUsageAttribute class is used to define custom attributes. When you create a custom attribute class, you can apply AttributeUsageAttribute to your attribute class to specify how the custom attribute can be used.
The AttributeUsageAttribute class has three properties:
AttributeUsageAttribute.AttributeUsageAttribute.AttributeTargets: Specifies the valid declaration in which the attribute can be applied.AttributeUsageAttribute.Inherited: Specifies whether the attribute is inherited by derived classes and overrides.AttributeUsageAttribute.AllowMultiple: Specifies whether the attribute can be applied more than once to the same declaration.
Constructors
- AttributeUsageAttribute(AttributeTargets)
- Initializes a new instance of the
AttributeUsageAttributeclass with specified attribute usage information.
Properties
- AllowMultiple (inherited from Attribute)
- Gets a value that indicates whether one or more instances of the attribute can be applied to the same assembly, type, or member.
- AttributeTargets
- Gets the declaration that is permitted to be decorated with the attribute.
- Inherited (inherited from Attribute)
- Gets a value that indicates whether the attribute is inherited by derived classes and overrides.
Methods
- GetHashCode() (inherited from Object)
- Returns the hash code for this instance.
- GetType() (inherited from Object)
- Gets the Type of the current instance.
- IsDefaultAttribute() (inherited from Attribute)
- Determines whether the specified attribute is the default attribute for this attribute type.
- Match(Object) (inherited from Attribute)
- When overridden in a derived class, determines whether the specified attribute matches the current attribute.
- ToString() (inherited from Object)
- Returns a string that represents the current object.
Example
The following example demonstrates how to create a custom attribute named CodeDescriptionAttribute and apply the AttributeUsageAttribute to it. This custom attribute can be applied to any code element, is not inherited, and can be applied multiple times.
using System;
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public class CodeDescriptionAttribute : Attribute
{
private string description;
public CodeDescriptionAttribute(string description)
{
this.description = description;
}
public string Description
{
get { return description; }
}
}
public class ExampleClass
{
[CodeDescription("This is a sample method.")]
[CodeDescription("Another description for the same method.")]
public void SampleMethod()
{
// Method implementation
}
}
Explanation:
AttributeTargets.All: The attribute can be applied to any declaration (class, method, property, etc.).Inherited = false: The attribute is not inherited by derived classes.AllowMultiple = true: The attribute can be applied more than once to the same declaration.