Accessing Attributes by Using Reflection (C#)

The fact that you can define custom attributes and place them in your source code would be of little value without some way of retrieving that information and acting on it. By using reflection, you can retrieve the information that was defined with custom attributes. The key method is GetCustomAttributes, which returns an array of objects that are the run-time equivalents of the source code attributes. This method has several overloaded versions. For more information, see Attribute.

An attribute specification such as:

[Author("P. Ackerman", version = 1.1)]  
class SampleClass  

is conceptually equivalent to this:

Author anonymousAuthorObject = new Author("P. Ackerman");  
anonymousAuthorObject.version = 1.1;  

However, the code is not executed until SampleClass is queried for attributes. Calling GetCustomAttributes on SampleClass causes an Author object to be constructed and initialized as above. If the class has other attributes, other attribute objects are constructed similarly. GetCustomAttributes then returns the Author object and any other attribute objects in an array. You can then iterate over this array, determine what attributes were applied based on the type of each array element, and extract information from the attribute objects.


Here is a complete example. A custom attribute is defined, applied to several entities, and retrieved via reflection.

// Multiuse attribute.  
[System.AttributeUsage(System.AttributeTargets.Class |  
                       AllowMultiple = true)  // Multiuse attribute.  
public class Author : System.Attribute  
    string name;  
    public double version;  

    public Author(string name)  
    {  = name;  

        // Default value.  
        version = 1.0;  

    public string GetName()  
        return name;  

// Class with the Author attribute.  
[Author("P. Ackerman")]  
public class FirstClass  
    // ...  

// Class without the Author attribute.  
public class SecondClass  
    // ...  

// Class with multiple Author attributes.  
[Author("P. Ackerman"), Author("R. Koch", version = 2.0)]  
public class ThirdClass  
    // ...  

class TestAuthorAttribute  
    static void Test()  

    private static void PrintAuthorInfo(System.Type t)  
        System.Console.WriteLine("Author information for {0}", t);  

        // Using reflection.  
        System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  // Reflection.  

        // Displaying output.  
        foreach (System.Attribute attr in attrs)  
            if (attr is Author)  
                Author a = (Author)attr;  
                System.Console.WriteLine("   {0}, version {1:f}", a.GetName(), a.version);  
/* Output:  
    Author information for FirstClass  
       P. Ackerman, version 1.00  
    Author information for SecondClass  
    Author information for ThirdClass  
       R. Koch, version 2.00  
       P. Ackerman, version 1.00  

See Also

C# Programming Guide
Retrieving Information Stored in Attributes
Reflection (C#)
Attributes (C#)
Creating Custom Attributes (C#)