< Day Day Up > |
TechniqueThe 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 AssemblyAssembly 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 > |