MSDN Documentation

Microsoft Developer Network

C# Attributes

Attributes are a form of declarative programming that allows you to add metadata to your code. This metadata can be examined at runtime using reflection, allowing you to alter the behavior of your code or provide information to tools and frameworks.

What are Attributes?

Attributes are classes that derive from the System.Attribute class. They provide a way to associate declarative information with program elements such as assemblies, types, methods, properties, and fields. This information is not part of the program element itself but is instead associated with it externally.

Applying Attributes

You apply attributes to program elements by placing the attribute name in square brackets, immediately preceding the declaration of the program element. Most attributes have a name that ends with the suffix Attribute. However, when applying an attribute, you can omit this suffix.

Example: Applying the `ObsoleteAttribute`

The ObsoleteAttribute is used to mark code elements that are no longer recommended for use, indicating that they will be removed in a future version of the product.


[System.Obsolete("This method is obsolete. Use NewMethod instead.")]
public void OldMethod()
{
    // ... old implementation ...
}

public void NewMethod()
{
    // ... new implementation ...
}
            

When code calls OldMethod, the compiler will issue a warning (or an error, if specified). You can also specify whether the attribute should cause a warning or an error:


[Obsolete("This feature will be removed in a future version.", true)] // true means it's an error
public class DeprecatedClass { }
            

Commonly Used Attributes

The `AttributeUsage` Attribute

The AttributeUsage attribute is itself an attribute that can be applied to custom attribute classes. It controls where and how your custom attributes can be applied.

It takes parameters that define:

Example: Defining a Custom Attribute

Let's create a simple custom attribute called AuthorAttribute.


[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property,
                AllowMultiple = false,
                Inherited = true)]
public class AuthorAttribute : Attribute
{
    public string Name { get; }
    public int Year { get; }

    public AuthorAttribute(string name, int year)
    {
        Name = name;
        Year = year;
    }
}

// Applying the custom attribute
[Author("Alice Smith", 2023)]
public class MyClass
{
    [Author("Bob Johnson", 2022)]
    public string MyProperty { get; set; }

    [Author("Alice Smith", 2023)]
    public void MyMethod()
    {
        // ...
    }
}
            

Reflection and Attributes

The power of attributes is often realized through reflection. You can use reflection to query for attributes applied to types, members, and other code elements at runtime.

Example: Reading Attribute Information

This example shows how to retrieve the AuthorAttribute information.


using System;
using System.Reflection;

public class AttributeReader
{
    public static void DisplayAuthorInfo(Type type)
    {
        Console.WriteLine($"Author info for type: {type.Name}");
        AuthorAttribute classAttribute = (AuthorAttribute)Attribute.GetCustomAttribute(type, typeof(AuthorAttribute));
        if (classAttribute != null)
        {
            Console.WriteLine($"  Class: {classAttribute.Name} ({classAttribute.Year})");
        }

        MethodInfo[] methods = type.GetMethods();
        foreach (MethodInfo method in methods)
        {
            object[] methodAttributes = method.GetCustomAttributes(typeof(AuthorAttribute), false);
            foreach (AuthorAttribute attr in methodAttributes)
            {
                Console.WriteLine($"  Method '{method.Name}': {attr.Name} ({attr.Year})");
            }
        }

        PropertyInfo[] properties = type.GetProperties();
        foreach (PropertyInfo prop in properties)
        {
            object[] propAttributes = prop.GetCustomAttributes(typeof(AuthorAttribute), false);
            foreach (AuthorAttribute attr in propAttributes)
            {
                Console.WriteLine($"  Property '{prop.Name}': {attr.Name} ({attr.Year})");
            }
        }
    }

    // Assume AuthorAttribute and MyClass are defined as above
    // public static void Main(string[] args)
    // {
    //     DisplayAuthorInfo(typeof(MyClass));
    // }
}
            

Benefits of Using Attributes