System.Attribute Class

public abstract class Attribute

Base Types

Object
  Attribute

Assembly

mscorlib

Library

BCL

Summary

Serves as the base class for custom attributes.

Description

All attributes, whether built-in or user-defined, derive directly or indirectly from Attribute. Attributes inherit certain default behaviors: the attribute might be associated with any target element (see AttributeTargets); might or might not be inherited by a derived element; and multiple instances might or might not be allowed on the same target element. These behaviors are specified using AttributeUsageAttribute.

[Note: An attribute is an annotation that can be placed on an element of source code and used to store application-specific information at compile time. This information is stored in the metadata and can be accessed either during application execution, through a process known as reflection, or when another tool reads the metadata. Attributes might change the behavior of the application during execution, provide transaction information about an object, or convey organizational information to a designer. ]

The CLI predefines some attribute types and uses them to control runtime behavior. Some languages predefine attribute types to represent language features not directly represented in the Common Language Specification (CLS). User-defined attribute classes, inheriting from Attribute, can also be created. The definition of such a class includes the name of the attribute, its default behavior, and the information to be stored.

Example

The following example creates and assigns multiple custom attributes to a class. The attribute contains the name of the programmer and the version number of the class.

using System;

[AttributeUsage(AttributeTargets.Class|
                AttributeTargets.Struct,
                AllowMultiple=true)]
public class Author : Attribute
{
   string authorName;
   public double verSion;

   public Author(string name) 
   {
      authorName = name;
      verSion = 1.0; 
   }

   public string getName() 
   {
      return authorName; 
   }
}

[Author("Some Author")]
class FirstClass 
{
   /*...*/ 
}

class SecondClass  // no Author attribute
{
   /*...*/ 
}

[Author("Some Author"), 
       Author("Some Other Author", verSion=1.1)]
class ThirdClass 
{ 
   /*...*/ 
}

class AuthorInfo 
{
   public static void Main() 
   {
      PrintAuthorInfo(typeof(FirstClass));
      PrintAuthorInfo(typeof(SecondClass));
      PrintAuthorInfo(typeof(ThirdClass));
   }
   public static void PrintAuthorInfo(Type type) 
   {
      Console.WriteLine("Author information for {0}",
                        type);
      Attribute[] attributeArray =
          Attribute.GetCustomAttributes(type);
      foreach(Attribute attrib in attributeArray) 
      {
         if (attrib is Author) 
         {
            Author author = (Author)attrib;
            Console.WriteLine("   {0}, version {1:f}",
                              author.getName(),
                              author.verSion);
         }
      }
      Console.WriteLine();
   }
}
      
The output is

Author information for FirstClass

Some Author, version 1.00

Author information for SecondClass

Author information for ThirdClass

Some Author, version 1.00

Some Other Author, version 1.10

Attributes

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

See Also

System Namespace

Members

Attribute Constructors

Attribute Constructor

Attribute Methods

Attribute.Equals Method
Attribute.GetCustomAttribute(System.Reflection.Assembly, System.Type) Method
Attribute.GetCustomAttribute(System.Reflection.Module, System.Type) Method
Attribute.GetCustomAttribute(System.Reflection.ParameterInfo, System.Type) Method
Attribute.GetCustomAttribute(System.Reflection.MemberInfo, System.Type) Method
Attribute.GetCustomAttributes(System.Reflection.Assembly) Method
Attribute.GetCustomAttributes(System.Reflection.Module) Method
Attribute.GetCustomAttributes(System.Reflection.ParameterInfo) Method
Attribute.GetCustomAttributes(System.Reflection.Assembly, System.Type) Method
Attribute.GetCustomAttributes(System.Reflection.Module, System.Type) Method
Attribute.GetCustomAttributes(System.Reflection.ParameterInfo, System.Type) Method
Attribute.GetCustomAttributes(System.Reflection.MemberInfo) Method
Attribute.GetCustomAttributes(System.Reflection.MemberInfo, System.Type) Method
Attribute.GetHashCode Method
Attribute.IsDefined(System.Reflection.MemberInfo, System.Type) Method
Attribute.IsDefined(System.Reflection.ParameterInfo, System.Type) Method
Attribute.IsDefined(System.Reflection.Module, System.Type) Method
Attribute.IsDefined(System.Reflection.Assembly, System.Type) Method


Attribute Constructor

protected Attribute();

Summary

Constructs a new instance of the Attribute class.

See Also

System.Attribute Class, System Namespace

Attribute.Equals Method

public override bool Equals(object obj);

Summary

Determines whether the current instance and the specified Object represent the same type and value.

Parameters

obj
The Object to compare to the current instance.

Return Value

A Boolean where true indicates obj represents the same type and value as the current instance. If obj is a null reference or is not an instance of Attribute, returns false .

Description

[Note: This method overrides System.Object.Equals(System.Object).

]

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttribute(System.Reflection.Assembly, System.Type) Method

public static Attribute GetCustomAttribute(Assembly element, Type attributeType);

Summary

Returns an instance of a specified custom attribute if a single instance of the attribute is in the metadata for the specified assembly.

Parameters

element
A Assembly instance.
attributeType
The Type of the custom attribute for which to check.

Return Value

The single instance of Attribute of type attributeType that is applied to element. Returns null if the specified attribute was not found.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or attributeType is null .
ArgumentExceptionattributeType is not a type derived from Attribute.
AmbiguousMatchExceptionMore than one instance of the specified custom attribute was found.

Description

[Note: If multiple instances of attributeType can be applied to element, use System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo,System.Type).]

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttribute(System.Reflection.Module, System.Type) Method

public static Attribute GetCustomAttribute(Module element, Type attributeType);

Summary

Returns an instance of a specified custom attribute if a single instance of the attribute is in the metadata for the specified module.

Parameters

element
A Module instance.
attributeType
The Type of the custom attribute for which to check.

Return Value

The single instance of Attribute of type attributeType that is applied to element. Returns null if the specified attribute was not found.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or attributeType is null .
ArgumentExceptionattributeType is not a type derived from Attribute.
AmbiguousMatchExceptionMore than one instance of the specified custom attribute was found.

Description

[Note: If multiple instances of attributeType can be applied to element, use System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo,System.Type).]

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttribute(System.Reflection.ParameterInfo, System.Type) Method

public static Attribute GetCustomAttribute(ParameterInfo element, Type attributeType);

Summary

Returns an instance of a specified custom attribute if a single instance of the attribute is in the metadata for the specified parameter.

Parameters

element
A ParameterInfo instance.
attributeType
The Type of the custom attribute for which to check.

Return Value

The single instance of Attribute of type attributeType that is applied to element. Returns null if the specified attribute was not found.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or attributeType is null .
ArgumentExceptionattributeType is not a type derived from Attribute.
AmbiguousMatchExceptionMore than one instance of the specified custom attribute was found.

Description

[Note: If multiple instances of attributeType can be applied to element, use System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo,System.Type).]

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttribute(System.Reflection.MemberInfo, System.Type) Method

public static Attribute GetCustomAttribute(MemberInfo element, Type attributeType);

Summary

Returns an instance of a specified custom attribute if a single instance of the attribute is in the metadata for the specified member.

Parameters

element
An instance of a type derived from MemberInfo that describes a type member.
attributeType
The Type of the custom attribute for which to check.

Return Value

The single instance of Attribute of type attributeType that is applied to element. Returns null if the specified attribute was not found.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or attributeType is null .
ArgumentExceptionattributeType is not a type derived from Attribute.
NotSupportedExceptionelement does not represent a constructor, method, property, event, type, or field member.
AmbiguousMatchExceptionMore than one instance of the specified custom attribute was found.

Description

[Note: If multiple instances of attributeType can be applied to element, use System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo,System.Type).]

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttributes(System.Reflection.Assembly) Method

public static Attribute[] GetCustomAttributes(Assembly element);

Summary

Returns an array of all custom attributes in the metadata for the specified assembly.

Parameters

element
A Assembly instance.

Return Value

A Attribute array containing all custom attributes that are applied to element. The array includes any inherited custom attributes. Returns an empty array if no custom attributes were found in the metadata for element .

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement is null .

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttributes(System.Reflection.Module) Method

public static Attribute[] GetCustomAttributes(Module element);

Summary

Returns an array of all custom attributes in the metadata for the specified module.

Parameters

element
A Module instance.

Return Value

A Attribute array containing all custom attributes that are applied to element. The array includes any inherited custom attributes. Returns an empty array if no custom attributes were found in the metadata for element .

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement is null .

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttributes(System.Reflection.ParameterInfo) Method

public static Attribute[] GetCustomAttributes(ParameterInfo element);

Summary

Returns an array of all custom attributes in the metadata for the specified parameter.

Parameters

element
A ParameterInfo instance.

Return Value

A Attribute array containing all custom attributes that are applied to element. The array includes any inherited custom attributes. Returns an empty array if no custom attributes were found in the metadata for element .

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement is null .

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttributes(System.Reflection.Assembly, System.Type) Method

public static Attribute[] GetCustomAttributes(Assembly element, Type attributeType);

Summary

Returns an array of the instances of a specified custom attribute if the attribute is in the metadata for the specified assembly.

Parameters

element
A Assembly instance.
attributeType
The Type of the custom attribute for which to check.

Return Value

An array of type attributeType containing the instances that are applied to element. The array includes any inherited instances of attributeType. Returns an empty array if the specified attribute was not found.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or type is null .
ArgumentExceptionattributeType is not a type derived from Attribute.

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttributes(System.Reflection.Module, System.Type) Method

public static Attribute[] GetCustomAttributes(Module element, Type attributeType);

Summary

Returns an array of the instances of a specified custom attribute if the attribute is in the metadata for the specified module.

Parameters

element
A Module instance.
attributeType
The Type of the custom attribute for which to check.

Return Value

An array of type attributeType containing the instances that are applied to element. The array includes any inherited instances of attributeType. Returns an empty array if the specified attribute was not found.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or type is null .
ArgumentExceptionattributeType is not a type derived from Attribute.

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttributes(System.Reflection.ParameterInfo, System.Type) Method

public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attributeType);

Summary

Returns an array of the instances of a specified custom attribute if the attribute is in the metadata for the specified parameter.

Parameters

element
A ParameterInfo instance.
attributeType
The Type of the custom attribute for which to check.

Return Value

An array of type attributeType containing the instances that are applied to element. The array includes any inherited instances of attributeType. Returns an empty array if the specified attribute was not found.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or attributeType is null .
ArgumentExceptionattributeType is not a type derived from Attribute.

Description

If element represents a method parameter, the array returned by System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo,System.Type) includes any attributeType instances for the parameter element in the base methods.

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttributes(System.Reflection.MemberInfo) Method

public static Attribute[] GetCustomAttributes(MemberInfo element);

Summary

Returns an array of all custom attributes in the metadata for the specified member.

Parameters

element
An instance of a type derived from MemberInfo that describes a type member.

Return Value

A Attribute array containing all custom attributes that are applied to element. The array includes custom attributes that are inherited by element, if any. Returns an empty array if no custom attributes were found in the metadata for element .

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement is null .
NotSupportedExceptionelement does not represent a constructor, method, property, event, type, or field member.

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetCustomAttributes(System.Reflection.MemberInfo, System.Type) Method

public static Attribute[] GetCustomAttributes(MemberInfo element, Type type);

Summary

Returns an array of the instances of a specified custom attribute if the attribute is in the metadata for the specified member.

Parameters

element
An instance of a type derived from MemberInfo that describes a type member.
type
The Type of the custom attribute for which to check.

Return Value

An array of type type containing the instances that are applied to element. The array includes instances of type that are inherited by element, if any. Returns an empty array if the specified attribute was not found.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or type is null .
ArgumentExceptiontype is not a type derived from Attribute.
NotSupportedExceptionelement does not represent a constructor, method, property, event, type, or field member.

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.GetHashCode Method

public override int GetHashCode();

Summary

Generates a hash code for the current instance.

Return Value

A Int32 containing the hash code for the current instance.

Description

The algorithm used to generate the hash code is unspecified.

[Note: This method overrides System.Object.GetHashCode. ]

See Also

System.Attribute Class, System Namespace

Attribute.IsDefined(System.Reflection.MemberInfo, System.Type) Method

public static bool IsDefined(MemberInfo element, Type attributeType);

Summary

Returns a Boolean value indicating whether a specified custom attribute is present in the metadata for the specified member.

Parameters

element
An instance of a type derived from MemberInfo that describes a type member.
attributeType
The Type of the custom attribute for which to check.

Return Value

true if a custom attribute of type attributeType is applied to element either directly or through inheritance; otherwise, false .

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or attributeType is null .
ArgumentExceptionattributeType is not derived from Attribute.
NotSupportedExceptionelement is not a constructor, method, property, event, type, or field.

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.IsDefined(System.Reflection.ParameterInfo, System.Type) Method

public static bool IsDefined(ParameterInfo element, Type attributeType);

Summary

Returns a Boolean value indicating whether a specified custom attribute is present in the metadata for the specified parameter.

Parameters

element
A ParameterInfo instance.
attributeType
The Type of the custom attribute for which to check.

Return Value

true if a custom attribute of type attributeType is applied to element either directly or through inheritance; otherwise, false .

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or attributeType is null .
ArgumentExceptionattributeType is not derived from Attribute.

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.IsDefined(System.Reflection.Module, System.Type) Method

public static bool IsDefined(Module element, Type attributeType);

Summary

Returns a Boolean value indicating whether a specified custom attribute is present in the metadata for the specified module.

Parameters

element
A Module instance.
attributeType
The Type of the custom attribute for which to check.

Return Value

true if a custom attribute of type attributeType is applied to element ; otherwise, false .

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or attributeType is null .
ArgumentExceptionattributeType is not derived from Attribute.

Library

Reflection

See Also

System.Attribute Class, System Namespace

Attribute.IsDefined(System.Reflection.Assembly, System.Type) Method

public static bool IsDefined(Assembly element, Type attributeType);

Summary

Returns a Boolean value indicating whether a specified custom attribute is present in the metadata for the specified assembly.

Parameters

element
A Assembly instance.
attributeType
The Type of the custom attribute for which to check.

Return Value

true if a custom attribute of type attributeType is applied to element; otherwise, false .

Exceptions

Exception TypeCondition
ArgumentNullExceptionelement or attributeType is null .
ArgumentExceptionattributeType is not derived from Attribute.

Library

Reflection

See Also

System.Attribute Class, System Namespace