Freigeben über


Accessing Custom Attributes

Microsoft Silverlight will reach end of support after October 2021. Learn more.

After attributes have been associated with program elements, reflection can be used to query their existence and values. The main reflection methods for retrieving custom attributes in Silverlight-based applications are Assembly.GetCustomAttributes and MemberInfo.GetCustomAttributes. These methods create instances of the attributes they find, which means that the assemblies that define those attributes must be loaded into the execution context.

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.

For example, suppose the internal (Friend in Visual Basic) MyDescriptionAttribute attribute in the MyAssembly assembly derives from DescriptionAttribute in System.dll.

System.DLL
public class DescriptionAttribute : Attribute
{
}

MyAssembly.DLL
internal class MyDescriptionAttribute : DescriptionAttribute
{
}

public class MyClass
{
    [MyDescriptionAttribute(...)]
    public void MyMethod(...)
    {
    }
}

If you use the MemberInfo.GetCustomAttributes(Type, Boolean) method overload to retrieve the custom attributes of MyMethod that derive from the public custom attribute type DescriptionAttribute, the runtime performs the following actions:

  1. Checks that the type argument DescriptionAttribute is public, and therefore is visible and accessible.

  2. Checks that the user-defined type MyDescriptionAttribute that is derived from DescriptionAttribute is visible and accessible within the MyAssembly assembly, where it is attached to the MyMethod method.

  3. Checks that the constructor of MyDescriptionAttribute is visible and accessible within the MyAssembly assembly.

  4. Calls the constructor of MyDescriptionAttribute with the custom attribute parameters and returns the new object.

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 GetMethods returning an array of RuntimeMethodInfo objects (see Runtime Types in Reflection). To prevent a client from obtaining instances of 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.

    Dim t As Type = GetType(Class1)
    Dim attrList As New StringBuilder()
    For Each attr As Object In t.GetCustomAttributes(True)
        attrList.AppendLine(attr.ToString())
    Next i
    myTextBlock.Text = attrList.ToString()
    Type t = typeof(MyClass);
    StringBuilder attrList = new StringBuilder();
    for each (object attr in .GetCustomAttributes(true))
    {
        attrList.Append(attr);
    }
    myTextBlock.Text = attrList.ToString();