An attribute in C# is a declarative tag used to associate metadata with program elements such as assemblies, types, methods, properties, and fields. During compilation, the C# compiler bakes this metadata into the assembly’s metadata tables, which are distinct from the Common Intermediate Language (CIL) instruction stream. This embedded metadata does not directly affect the execution of the code but can be inspected and acted upon at runtime using Reflection.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Syntax
Attributes are applied by enclosing the attribute name and its arguments in square brackets[] immediately above the target element.
Attribute. The C# compiler allows you to omit this suffix when applying the attribute in code (e.g., [Obsolete] instead of [ObsoleteAttribute]).
Anatomy of a Custom Attribute
Under the hood, an attribute is simply a class that inherits directly or indirectly from theSystem.Attribute base class.
Parameter Types
Attribute parameters are strictly limited by the compiler. They can only be:- Simple types (
bool,byte,sbyte,char,short,ushort,int,uint,long,ulong,float,double) stringSystem.Type- Enums
object(though the actual argument passed at compile-time must evaluate to one of the other permitted types)- Single-dimensional arrays of any of the above
Positional vs. Named Parameters
When applying an attribute, arguments are passed based on the attribute class’s design:- Positional Parameters: These map directly to the constructor signatures of the attribute class. They are mandatory and must be specified in the exact order defined by the constructor.
- Named Parameters: These map to public, non-static, read-write fields or properties on the attribute class. They are optional and are specified using the
PropertyName = valuesyntax after all positional parameters.
The [AttributeUsage] Meta-Attribute
When defining a custom attribute, its behavior and valid application targets are governed by applying the System.AttributeUsageAttribute to the custom attribute class itself. It accepts three configurations:
- ValidOn (
AttributeTargets): A bitwise combination ofAttributeTargetsenum values (e.g.,Class,Method,Property,All) dictating exactly which code elements the compiler will allow the attribute to be attached to. - AllowMultiple (
bool): Determines whether the same attribute can be applied more than once to a single program element. - Inherited (
bool): Determines whether the attribute is automatically inherited by derived classes or overriding methods.
Metadata Retrieval
Because attributes are compiled into the assembly’s metadata tables, they remain dormant until explicitly queried. The .NET runtime provides Reflection APIs, primarily through theSystem.Reflection.MemberInfo.GetCustomAttributes method, to instantiate and read the attribute objects at runtime.
Master C# with Deep Grasping Methodology!Learn More





