25.7 Displaying Assembly Custom Attribute Information Using Reflection

 <  Day Day Up  >  

You want to use reflection to display information about custom attributes that were applied to an assembly.


Technique

The technique is almost identical to that demonstrated in the preceding recipe to obtain custom attributes for a class ”except that we need to obtain an Assembly reference and call Assembly.GetCustomAttributes() instead of Type.GetCustomAttributes() . Listing 25.18 demonstrates the technique used to list the attributes that were applied to the currently executing assembly.

Listing 25.18 Listing the Attributes Applied to the Currently Executing Assembly
 Assembly assbly = Assembly.GetExecutingAssembly(); object [] attribs = assbly.GetCustomAttributes(true); foreach (object attrib in attribs) {         Console.WriteLine(attrib.ToString()); } 

Just like Type.GetCustomAttributes() , Assembly.GetCustomAttributes() takes a bool parameter. Although in theory, this parameter indicates whether the inheritance chain should be searched for attributes, assemblies don't actually have an inheritance chain. Hence, the parameter is completely ignored: you can set it to either true or false . The parameter is only present in this case to satisfy the signature requirements of the ICustomAttributeProvider interface, which Assembly implements.

More realistically , you might want to load an assembly specifically to examine its custom attributes:

 
 Assembly assbly = Assembly.LoadFrom(@"c:\My Projects\MyAssembly.dll"); object [] attribs = assbly.GetCustomAttributes(true); // etc. 

Chapter 19, "Assemblies," described the main techniques for loading an assembly and acquiring an Assembly reference.

 <  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