< Day Day Up > |
TechniqueTo access information about custom attributes, follow these steps:
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 ClassType 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 MethodType 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. CommentsIn 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 > |