Attributes (C# Programming Guide)
Updated: July 2008
Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection. For more information, see Reflection (C# Programming Guide).
Attributes occur in two forms:
Attributes that are defined in the common language runtime (CLR).
Custom attributes that you can create, to add extra information to your code. This information can later be retrieved programmatically.
In this example, the TypeAttributes.Serializable attribute is used to apply a specific characteristic to a class:
Attributes have the following properties:
Attributes add metadata to your program. Metadata is information about the types defined in a program. All .NET assemblies contain a specified set of metadata that describe the types and type members defined in the assembly. You can add custom attributes to specify any additional information that is required.
Your program can examine its own metadata or the metadata in other programs by using reflection. For more information, see Accessing Attributes With Reflection (C# Programming Guide).
Attributes such as MarshallAsare used extensively in COM interop scenarios. For more information, see System.Runtime.InteropServices.MarshalAsAttribute and System.Runtime.InteropServices.StructLayoutAttribute.
For more information, see:
For more information, see the following sections in the C# Language Specification: