25.8 Displaying Properties of an Applied Attribute at Runtime

 <  Day Day Up  >  

You want to display the values of specific properties of attributes that were applied to code items.


Technique

To display the values of attribute properties, you clearly need to know the type of the attribute you are expecting. In general, the technique is to simply call CustomAttributeProvider.GetCustomAttributes() and then cast the attributes returned to the required type and extract the relevant properties. To make this task easier, you can use an alternative overload of GetCustomAttributes() , which takes two parameters and allows you to restrict the returned array to contain only attributes that are castable to a specified type:

 
 object [] attribs = type.GetCustomAttributes(typeof(MyCustomAttribute), true); 

When using this overload, the first parameter is a Type reference that indicates the type of the attribute you want to be returned, and the second parameter is the usual bool that specifies whether you want to search the attributed item's inheritance chain for attributes. For example, suppose we are searching for occurrences of the ProgrammerTeam attribute that was presented in Listing 25.5. Recall that this attribute exposes two properties, InHouse (a bool ) and Team (a string ), and we want to display the values of these properties. Listing 25.19 shows how to do so. This sample assumes that the variable codeItem refers to some reflection object that implements ICustomAttributeProvider .

Listing 25.19 Using Reflection to Display Values of Attribute Properties
 object [] attribs = codeItem.GetCustomAttributes(typeof(ProgrammerTeamAttribute), true); foreach (object attrib in attribs) {         ProgrammerTeamAttribute progTeam = (ProgrammerTeamAttribute)attrib;         Console.WriteLine("Programmer team attribute values:");         Console.WriteLine("InHouse: " + progTeam.InHouse.ToString());         Console.WriteLine("Team:    " + progTeam.Team); } 

If in Listing 25.19, codeItem holds a type reference to a class defined like this:

 
 [InHouse()] [ProgrammerTeam(true, "Dave coded up this class")] class OurClass { 

Then, the result of running Listing 25.19 is this output:

 
 Programmer team attribute values: InHouse: True Team:    Dave coded up this class 

Comments

You should be aware that the code listings in this recipe presume that it is safe to cast the attributes returned from GetCustomAttributes() to the type we are interested in. If we use the two-parameter overload of GetCustomAttributes() , then that is the case because only attributes of the given type are returned. More generally , however, you might have a list of attributes returned from the one-parameter overload of GetCustomAttributes() , and you cannot be certain of the type of the attributes. In that case, you can use the C# as operator to pick out the attributes you are interested in:

 
 object [] attribs = codeItem.GetCustomAttributes(true); foreach (object attrib in attribs) {         // to pick out any instances of MyCustomAttribute         ProgrammerTeamAttribute myCustomAttrib = attrib as MyCustomAttribute;         if (myCustomAttrib != null)                 // do something with the attribute } 

In the most general case, you might not know at compile time what attributes are going to turn up, and you just want to display all the properties of whatever attributes you find. In this case, you need to use reflection over again to identify the type of each attribute and then iterate over its properties using the techniques described in Chapter 23:

 
 object [] attribs = codeItem.GetCustomAttributes(true); foreach (object attrib in attribs) {         Type t = attrib.GetType();         PropertyInfo [] properties = t.GetProperties();         foreach (Property property in properties)         {                 // process this property         } } 
 <  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