System.Attribute Class

Represents the base class for custom attributes.

See Also: Attribute

Syntax

[System.AttributeUsage(System.AttributeTargets.All)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComDefaultInterface(typeof(System.Runtime.InteropServices._Attribute))]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Attribute : _Attribute

Remarks

The Attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute.

Information provided by an attribute is also known as metadata. Metadata can be examined at run time by your application to control how your program processes data, or before run time by external tools to control how your application itself is processed or maintained. For example, the .NET Framework predefines and uses attribute types to control run-time behavior, and some programming languages use attribute types to represent language features not directly supported by the .NET Framework common type system.

All attribute types derive directly or indirectly from the Attribute class. Attributes can be applied to any target element; multiple attributes can be applied to the same target element; and attributes can be inherited by an element derived from a target element. Use the AttributeTargets class to specify the target element to which the attribute is applied.

The Attribute class provides convenient methods to retrieve and test custom attributes. For more information about using attributes, see Applying Attributes and Extending Metadata Using Attributes.

Thread Safety

This type is safe for multithreaded operations.

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.

C# Example

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

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0

The members of System.Attribute are listed below.

See Also: Object

Protected Constructors

Initializes a new instance of the Attribute class.

Public Properties

[read-only]
TypeIdObject.

When implemented in a derived class, gets a unique identifier for this Attribute.

Public Methods

override
Equals(Object) : Boolean

Returns a value that indicates whether this instance is equal to a specified object.

static
GetCustomAttribute(Assembly, Type) : Attribute

Retrieves a custom attribute applied to a specified assembly. Parameters specify the assembly and the type of the custom attribute to search for.

static
GetCustomAttribute(MemberInfo, Type) : Attribute

Retrieves a custom attribute applied to a member of a type. Parameters specify the member, and the type of the custom attribute to search for.

static
GetCustomAttribute(Module, Type) : Attribute

Retrieves a custom attribute applied to a module. Parameters specify the module, and the type of the custom attribute to search for.

static
GetCustomAttribute(ParameterInfo, Type) : Attribute

Retrieves a custom attribute applied to a method parameter. Parameters specify the method parameter, and the type of the custom attribute to search for.

static
GetCustomAttribute(Assembly, Type, Boolean) : Attribute

Retrieves a custom attribute applied to an assembly. Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.

static
GetCustomAttribute(MemberInfo, Type, Boolean) : Attribute

Retrieves a custom attribute applied to a member of a type. Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.

static
GetCustomAttribute(Module, Type, Boolean) : Attribute

Retrieves a custom attribute applied to a module. Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.

static
GetCustomAttribute(ParameterInfo, Type, Boolean) : Attribute

Retrieves a custom attribute applied to a method parameter. Parameters specify the method parameter, the type of the custom attribute to search for, and whether to search ancestors of the method parameter.

static
GetCustomAttributes(Assembly) : Attribute[]

Retrieves an array of the custom attributes applied to an assembly. A parameter specifies the assembly.

static
GetCustomAttributes(MemberInfo) : Attribute[]

Retrieves an array of the custom attributes applied to a member of a type. A parameter specifies the member.

static
GetCustomAttributes(Module) : Attribute[]

Retrieves an array of the custom attributes applied to a module. A parameter specifies the module.

static
GetCustomAttributes(ParameterInfo) : Attribute[]

Retrieves an array of the custom attributes applied to a method parameter. A parameter specifies the method parameter.

static
GetCustomAttributes(Assembly, Boolean) : Attribute[]

Retrieves an array of the custom attributes applied to an assembly. Parameters specify the assembly, and an ignored search option.

static
GetCustomAttributes(Assembly, Type) : Attribute[]

Retrieves an array of the custom attributes applied to an assembly. Parameters specify the assembly, and the type of the custom attribute to search for.

static
GetCustomAttributes(MemberInfo, Boolean) : Attribute[]

Retrieves an array of the custom attributes applied to a member of a type. Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.

static
GetCustomAttributes(MemberInfo, Type) : Attribute[]

Retrieves an array of the custom attributes applied to a member of a type. Parameters specify the member, and the type of the custom attribute to search for.

static
GetCustomAttributes(Module, Boolean) : Attribute[]

Retrieves an array of the custom attributes applied to a module. Parameters specify the module, and an ignored search option.

static
GetCustomAttributes(Module, Type) : Attribute[]

Retrieves an array of the custom attributes applied to a module. Parameters specify the module, and the type of the custom attribute to search for.

static
GetCustomAttributes(ParameterInfo, Boolean) : Attribute[]

Retrieves an array of the custom attributes applied to a method parameter. Parameters specify the method parameter, and whether to search ancestors of the method parameter.

static
GetCustomAttributes(ParameterInfo, Type) : Attribute[]

Retrieves an array of the custom attributes applied to a method parameter. Parameters specify the method parameter, and the type of the custom attribute to search for.

static
GetCustomAttributes(Assembly, Type, Boolean) : Attribute[]

Retrieves an array of the custom attributes applied to an assembly. Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.

static
GetCustomAttributes(MemberInfo, Type, Boolean) : Attribute[]

Retrieves an array of the custom attributes applied to a member of a type. Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.

static
GetCustomAttributes(Module, Type, Boolean) : Attribute[]

Retrieves an array of the custom attributes applied to a module. Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.

static
GetCustomAttributes(ParameterInfo, Type, Boolean) : Attribute[]

Retrieves an array of the custom attributes applied to a method parameter. Parameters specify the method parameter, the type of the custom attribute to search for, and whether to search ancestors of the method parameter.

override
GetHashCode() : Int32

Returns the hash code for this instance.

IsDefaultAttribute() : Boolean

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

static
IsDefined(Assembly, Type) : Boolean

Determines whether any custom attributes are applied to an assembly. Parameters specify the assembly, and the type of the custom attribute to search for.

static
IsDefined(MemberInfo, Type) : Boolean

Determines whether any custom attributes are applied to a member of a type. Parameters specify the member, and the type of the custom attribute to search for.

static
IsDefined(Module, Type) : Boolean

Determines whether any custom attributes of a specified type are applied to a module. Parameters specify the module, and the type of the custom attribute to search for.

static
IsDefined(ParameterInfo, Type) : Boolean

Determines whether any custom attributes are applied to a method parameter. Parameters specify the method parameter, and the type of the custom attribute to search for.

static
IsDefined(Assembly, Type, Boolean) : Boolean

Determines whether any custom attributes are applied to an assembly. Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.

static
IsDefined(MemberInfo, Type, Boolean) : Boolean

Determines whether any custom attributes are applied to a member of a type. Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.

static
IsDefined(Module, Type, Boolean) : Boolean

Determines whether any custom attributes are applied to a module. Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.

static
IsDefined(ParameterInfo, Type, Boolean) : Boolean

Determines whether any custom attributes are applied to a method parameter. Parameters specify the method parameter, the type of the custom attribute to search for, and whether to search ancestors of the method parameter.

Match(Object) : Boolean

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

Explicitly Implemented Interface Members