Share via


Accessing Custom AttributesĀ 

After attributes have been associated with program elements, reflection can be used to query their existence and values. In the .NET Framework version 1.0 and 1.1, custom attributes are examined in the execution context. The .NET Framework version 2.0 provides a new load context, the reflection-only context, which can be used to examine code that cannot be loaded for execution.

The Reflection-Only Context

Code loaded into the reflection-only context cannot be executed. This means that instances of custom attributes cannot be created, because that would require executing their constructors. To load and examine custom attributes in the reflection-only context, use the CustomAttributeData class. You can obtain instances of this class by using the appropriate overload of the static System.Reflection.CustomAttributeData.GetCustomAttributes method. See How to: Load Assemblies into the Reflection-Only Context.

The Execution Context

The main reflection methods to query attributes in the execution context are System.Reflection.MemberInfo.GetCustomAttributes and System.Reflection.Assembly.GetCustomAttributes.

The accessibility of a custom attribute is checked with respect to the assembly in which it is attached. This is equivalent to checking whether a method on a type in the assembly in which the custom attribute is attached can call the constructor of the custom attribute.

Methods such as System.Reflection.Assembly.GetCustomAttributes(Type, Boolean) check the visibility and accessibility of the type argument. Only code in the assembly that contains the user-defined type can retrieve a custom attribute of that type using GetCustomAttributes.

The following C# example is a typical custom attribute design pattern. It illustrates the runtime custom attribute reflection model.

System.DLL
public class DescriptionAttribute : Attribute
{
}

System.Web.DLL
internal class MyDescriptionAttribute : DescriptionAttribute
{
}

public class LocalizationExtenderProvider
{
    [MyDescriptionAttribute(...)]
    public CultureInfo GetLanguage(...)
    {
    }
}

If the runtime is attempting to retrieve the custom attributes for the public custom attribute type DescriptionAttribute attached to the GetLanguage method, it performs the following actions:

  1. The runtime checks that the type argument DescriptionAttribute to Type.GetCustomAttributes(Type type) is public, and therefore is visible and accessible.

  2. The runtime checks that the user-defined type MyDescriptionAttribute that is derived from DescriptionAttribute is visible and accessible within the System.Web.DLL assembly, where it is attached to the method GetLanguage().

  3. The runtime checks that the constructor of MyDescriptionAttribute is visible and accessible within the System.Web.DLL assembly.

  4. The runtime calls the constructor of MyDescriptionAttribute with the custom attribute parameters and returns the new object to the caller.

The custom attribute reflection model could leak instances of user-defined types outside the assembly in which the type is defined. This is no different from the members in the runtime system library that return instances of user-defined types, such as Type.GetMethods() returning an array of RuntimeMethodInfo objects. To prevent a client from discovering information about a user-defined custom attribute type, define the type's members to be nonpublic.

The following example demonstrates the basic way of using reflection to get access to custom attributes.

Class Class1
    Public Shared Sub Main()
        Dim info As System.Reflection.MemberInfo = GetType(Class1)
        Dim attributes() As Object = info.GetCustomAttributes(True)
        For i As Integer = 0 To attributes.Length - 1
            System.Console.WriteLine(attributes(i))
        Next i
    End Sub
End Class
class MyClass 
{ 
    public static void Main() 
    {
        System.Reflection.MemberInfo info = typeof(MyClass);
        object[] attributes = info.GetCustomAttributes(true);
        for (int i = 0; i < attributes.Length; i++)
        {
            System.Console.WriteLine(attributes[i]);
        }
    }
}

See Also

Reference

MemberInfo.GetCustomAttributes
Attribute.GetCustomAttributes

Concepts

Viewing Type Information
Security Considerations for Reflection