Attributes provide a powerful way to associate metadata, or declarative information, with code (assemblies, types, methods, properties, and so on). After you associate an attribute with a program entity, you can query the attribute at run time by using a technique called reflection.
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 describes the types and type members defined in the assembly. You can add custom attributes to specify any other required information.
Attributes can be applied to entire assemblies, modules, or smaller program elements, such as classes and properties.
Attributes can accept arguments in the same way as methods and properties.
Attributes enable a program to examine its own metadata or metadata in other programs by using reflection.
Work with reflection
Reflection APIs provided by Type describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. When you use attributes in your code, reflection enables you to access them. For more information, see Attributes.
Here's a simple example of reflection with the GetType() method. All types from the Object base class inherit this method, which is used to obtain the type of a variable:
Note
Make sure you add the using System; and using System.Reflection; statements at the top of your C# (.cs) code file.
// Using GetType to obtain type information:
int i = 42;
Type type = i.GetType();
Console.WriteLine(type);
The output shows the type:
System.Int32
The following example uses reflection to obtain the full name of the loaded assembly.
// Using Reflection to get information of an Assembly:
Assembly info = typeof(int).Assembly;
Console.WriteLine(info);
The C# keywords protected and internal have no meaning in Intermediate Language (IL) and aren't used in the reflection APIs. The corresponding terms in IL are Family and Assembly. Here some ways you can use these terms:
To identify an internal method by using reflection, use the IsAssembly property.
Attributes can be placed on almost any declaration, though a specific attribute might restrict the types of declarations on which it's valid. In C#, you specify an attribute by placing the name of the attribute enclosed in square brackets ([]) above the declaration of the entity to which it applies.
In this example, you use the SerializableAttribute attribute to apply a specific characteristic to a class:
[Serializable]
public class SampleClass
{
// Objects of this type can be serialized.
}
By convention, all attribute names end with the suffix "Attribute" to distinguish them from other types in the .NET libraries. However, you don't need to specify the attribute suffix when you use attributes in code. For example, a [DllImport] declaration is equivalent to a [DllImportAttribute] declaration, but DllImportAttribute is the actual name of the class in the .NET Class Library.
Attribute parameters
Many attributes have parameters, which can be positional, unnamed, or named. The following table describes how to work with named and positional attributes:
Positional parameters
Parameters of the attribute constructor:
Named parameters
Properties or fields of the attribute:
Must specify, can't omit
Always specify first
Specify in certain order
Always optional, omit when false
Specify after positional parameters
Specify in any order
For example, the following code shows three equivalent DllImport attributes:
The first parameter, the DLL name, is positional and always comes first. The other instances are named parameters. In this scenario, both named parameters default to false, so they can be omitted. Refer to the individual attribute's documentation for information on default parameter values. For more information on allowed parameter types, see the Attributes section of the C# language specification.
Attribute targets
The target of an attribute is the entity that the attribute applies to. For example, an attribute can apply to a class, a method, or an assembly. By default, an attribute applies to the element that follows it. But you can also explicitly identify the element to associate, such as a method, a parameter, or the return value.
To explicitly identify an attribute target, use the following syntax:
[target : attribute-list]
The following table shows the list of possible target values.
Target value
Applies to
assembly
Entire assembly
module
Current assembly module
field
Field in a class or a struct
event
Event
method
Method or get and set property accessors
param
Method parameters or set property accessor parameters
property
Property
return
Return value of a method, property indexer, or get property accessor
The following example shows how to apply attributes to assemblies and modules. For more information, see Common attributes (C#).
using System;
using System.Reflection;
[assembly: AssemblyTitleAttribute("Production assembly 4")]
[module: CLSCompliant(true)]
The following example shows how to apply attributes to methods, method parameters, and method return values in C#.
// default: applies to method
[ValidatedContract]
int Method1() { return 0; }
// applies to method
[method: ValidatedContract]
int Method2() { return 0; }
// applies to parameter
int Method3([ValidatedContract] string contract) { return 0; }
// applies to return value
[return: ValidatedContract]
int Method4() { return 0; }
Note
Regardless of the targets on which the ValidatedContract attribute is defined to be valid, the return target has to be specified, even if the ValidatedContract attribute is defined to apply only to return values. In other words, the compiler doesn't use the AttributeUsage information to resolve ambiguous attribute targets. For more information, see AttributeUsage.
Review ways to use attributes
Here are some common ways to use attributes in code:
Mark controller methods that respond to POST messages by using the HttpPost attribute. For more information, see the HttpPostAttribute class.
Describe how to marshal method parameters when interoperating with native code. For more information, see the MarshalAsAttribute class.
Describe Component Object Model (COM) properties for classes, methods, and interfaces.
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Learn how attributes work in C#. You define custom attributes to add metadata to your code. You read those attributes to learn about the code at runtime
This chapter covers attributes. Attributes are metadata types that can be applied to many program elements: classes, structs, assemblies, members, and arguments.
This advanced tutorial explores how you can safely add new capabilities to existing interface definitions without breaking all classes and structs that implement that interface.