25.6 Displaying Custom Attribute Information Using Reflection

 < Day Day Up > 

You want to use reflection to display custom attribute information.


Technique

To access information about custom attributes, follow these steps:

  1. Use reflection to obtain a reference to a reflection-based class that describes the item you believe has been decorated with a custom attribute. For example, for a type, it will be a System.Type instance; for a method, it will be a System.Reflection.MethodInfo instance; and for an assembly, it will be a System.Reflection.Assembly instance.

  2. Invoke the ICustomAttributeProvider.GetCustomAttributes() method on the reflection object. This method actually instantiates all the attributes that are applied to the item and returns them in an object[] array. You will find that all the relevant reflection types, including Type , MethodInfo , MemberInfo , ParameterInfo , Assembly , and so on, implement the ICustomAttributeProvider interface and hence implement the GetCustomAttributes() method.

  3. Cast each attribute to the required type and extract the information you need using the various methods and properties implemented by the attribute class.

It's not really possible to give a specific set of instructions for Step 1 of this procedure because it depends on whether you want to examine the attributes applied to a class, to an assembly, to a method, to a parameter, or to some other item of code. Chapter 23, "Reflection," gave specific instructions for obtaining a reflection-based object in the various different cases. However, we provide a couple of examples to illustrate the general principles here.

We start by considering how to obtain attributes for a type: Let's suppose a type called MyClass has been defined somewhere. We want to list the attributes that might have been applied to this type. Listing 25.16 shows how we could do so.

Listing 25.16 Displaying the Attributes Applied to a Class
 Type type = typeof(MyClass); Console.WriteLine("Attributes defined for MyClass are:"); object [] attribs = type.GetCustomAttributes(true); foreach (object attrib in attribs) {         Console.WriteLine(attrib.ToString()); } 

Listing 25.16 shows that we first obtain a type reference to the type whose attributes we want to examine. Note that there is no restriction on how we obtain the Type reference. For example, if, instead of knowing the name of the type at compile time, you have an object reference, myObject , whose type-level attributes you want to examine, then you simply replace the first line of Listing 25.16 with

 
 Type type = myObject.GetType(); 

Having obtained a Type reference, we invoke the Type.GetCustomAttributes() method to get a list of the attributes. This method instantiates all the attributes and returns them as an array. The returned array is of type object[] because there is no way to predict in advance what the types of the attributes will be. GetCustomAttributes() takes one parameter, which indicates whether the inheritance chain is searched when retrieving attributes. If you pass in the value false , then the returned object[] array contains only attributes that have been explicitly assigned to this type. If you pass in true , then the array also contains any attributes that were applied to base types and for which the AttributeUsage.Inherited property was set to true .

Next, we simply iterate through the returned array, manipulating the attributes as desired. In Listing 25.16, we display the string representation of each attribute. Suppose, for example, that MyClass is defined in the following way:

 
 [InHouse()] [ProgrammerTeam(true)] class MyClass {         // etc. 

Then, assuming that ProgrammerTeamAttribute and InHouseAttribute are both defined in a namespace called TestAttributes , running Listing 25.16 generates the following results:

 
 Attributes defined for MyClass are: TestAttributes.ProgrammerTeamAttribute TestAttributes.InHouseAttribute 

On the other hand, suppose you want to obtain references to the attributes that were applied to a method. For the sake of argument, let's assume you want to examine the attributes applied to a method MyClass.DoSomething() . The relevant code appears in Listing 25.17.

Listing 25.17 Obtaining Object References for the Attributes Applied to a Method
 Type type = typeof(MyClass); MethodInfo method = type.GetMethod("DoSomething"); object [] attribs = method.GetCustomAttributes(false); foreach (object attrib in attribs) {         // do something with the attribute object reference, for example:         Console.WriteLine(attrib.ToString()); } 

This code shows that we simply obtain a MethodInfo reference that describes the method we are interested in. Then, the procedure is to call ICustomAttributeProvider.GetCustomAttributes() , just as for a class. Similar principles apply for obtaining the attributes of any other code item.

Comments

In this recipe, the emphasis was on displaying information about any custom attributes that you defined. However, you should bear in mind that the Common Language Runtime (CLR) makes no distinction between attributes that you define and custom attributes that Microsoft defines, such as ObsoleteAttribute or BrowsableAttribute . On the other hand, certain common Microsoft-supplied attributes, such as DllImportAttribute and StructLayoutAttribute , are not really custom attributes at all but are pseudo attributes. Pseudo attributes are removed by the C# compiler and replaced by various intermediate language (IL) flags, so they are not normally picked up by reflection.

 < Day Day Up > 


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net