System.Attribute Class

Represents the base class for custom attributes.

See Also: Attribute Members

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 : System.Runtime.InteropServices._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