System.Attribute Class

Represents the base class for custom attributes.

See Also: Attribute Members


public abstract class Attribute : System.Runtime.InteropServices._Attribute


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.


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;

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() 
   public static void PrintAuthorInfo(Type type) 
      Console.WriteLine("Author information for {0}",
      Attribute[] attributeArray =
      foreach(Attribute attrib in attributeArray) 
         if (attrib is Author) 
            Author author = (Author)attrib;
            Console.WriteLine("   {0}, version {1:f}",

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


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