C# Reference ยท Reflection, Attributes and Metadata
C# attributes
Reference for C# attributes: attach metadata to types and members with square brackets, set targets, pass arguments, and read attributes through reflection.
An attribute is a piece of metadata attached to a program element: an assembly, type, member, parameter or return value. You write it in square brackets above the element. The compiler stores it in the assembly, and tools, the runtime or your own code can read it later. Attributes do not change what the code does by themselves; something has to look at them.
Some attributes are understood by the compiler ([Obsolete], [Conditional]), some by the runtime ([StructLayout], [DllImport]), and some only by libraries and frameworks ([Serializable], web routing, test discovery). You can also write your own.
[Obsolete("Use NewMethod")] public void OldMethod() { } [Required, MaxLength(20)] // several attributes in one pair of brackets public string? Name { get; set; }
Try it
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.
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
[AttributeName] [AttributeName(positionalArg1, positionalArg2, NamedProperty = value)] [target: AttributeName] // assembly, module, type, method, field, property, event, param, return, typevar
| Part | Description |
|---|---|
| Name | The class name, with or without the Attribute suffix: [Obsolete] means ObsoleteAttribute. |
| Positional arguments | Passed to a constructor of the attribute class, in order. |
| Named arguments | Set public fields or properties of the attribute after construction. |
| Target specifier | assembly:, return:, field:, property:, param:, method:, type:, event: and typevar:. Needed when the default target is ambiguous. |
Reading: GetCustomAttribute<T>() returns one instance or null; GetCustomAttributes<T>() returns all; IsDefined only checks presence. These live on MemberInfo, Assembly, ParameterInfo and as static helpers on Attribute.
Usage notes
- Argument types are restricted. Attribute arguments must be compile-time constants,
typeofexpressions, arrays of those, or enums. Arbitrary objects,decimaland lambdas are not allowed. - Attributes are created on demand. The compiler stores the arguments; the attribute object is built each time you call
GetCustomAttribute. Cache results if you query often. - Auto-property targets:
[field: NonSerialized]on an auto-property applies the attribute to the hidden backing field. - Inheritance:
GetCustomAttribute(inherit: true)on a type searches base types only when the attribute allows it (AttributeUsage.Inherited). Property and method inheritance rules differ; test the case you rely on. - Metadata only: an attribute does nothing until a tool reads it. A missing validator or serializer is the usual reason an attribute "does not work".
- Trimming: attributes on members that are trimmed away disappear with them. See Native AOT and trimming.
- Generic attributes (
[MyAttr<int>]) have been allowed since C# 11: see Generic attributes.