Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Reflection, Attributes and Metadata

Writing custom attributes

Reference for writing custom attributes in C#: derive from Attribute, restrict targets with AttributeUsage, accept arguments and read them via reflection.

A custom attribute is an ordinary class that derives from System.Attribute. By convention its name ends in Attribute, and you apply it without that suffix. The class holds the data; the code that cares about the attribute reads it with reflection and acts on it.

Typical uses are marking classes for a plugin loader, describing validation rules, mapping properties to column names or flagging test methods. The [AttributeUsage] attribute on your attribute class says where it may be applied, whether it can repeat and whether derived types inherit it.

C#
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class ColumnAttribute(string name) : Attribute
{
    public string Name { get; } = name;
}

Try it

C# A mini validator driven by attributes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# AllowMultiple, Inherited and named arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
[AttributeUsage(AttributeTargets validOn, AllowMultiple = false, Inherited = true)]
public class MyAttribute : Attribute { }
Name Description
validOn Elements the attribute may be placed on: Class, Method, Property, Field, Parameter, Assembly, All and so on, combined with |.
AllowMultiple Whether the attribute can appear more than once on the same element. Default false.
Inherited Whether derived classes (or overriding members) see the attribute. Default true.

Applying an attribute to an element outside validOn, or twice without AllowMultiple, is a compile error (CS0592 and CS0579).

Usage notes

  • Constructor parameters become positional arguments; settable members become named ones. Use init or set properties for optional values.
  • Keep attributes dumb. Store data only. Put behaviour in the code that reads the attribute, because attribute constructors run whenever someone queries them and can throw at odd moments.
  • Seal your attribute class unless you need derivation; sealed attributes are faster to search for.
  • Parameter types are limited to constants, typeof, enums, strings, primitives and one-dimensional arrays of these.
  • Reading costs: GetCustomAttribute allocates new instances every call. Cache per type (a static dictionary or ConditionalWeakTable).
  • Prefer source generators for hot paths. Reading attributes at run time is flexible but slow and trimming-unfriendly; a source generator can do the same lookup at compile time.
  • Generic attribute classes such as RouteAttribute<T> are allowed since C# 11, with the limit that type arguments must be fully known, not type parameters of the enclosing type.