< Day Day Up > |
TechniqueIdentity information is available via the System.Reflection.AssemblyName class. Hence, you need to obtain an AssemblyName instance that represents the required assembly and then use various properties on this class to obtain the details of the assembly. If an assembly is loaded and you have an Assembly reference, then you can use the Assembly.GetName() method to retrieve an AssemblyName instance: // assume someAssembly is an Assembly reference AssemblyName name = someAssembly.GetName(); However, the neat thing about AssemblyName is that you can get an AssemblyName object without having to load the assembly concerned : There is a static method, AssemblyName.GetAssemblyName() , which opens an assembly file, reads the manifest from that file, extracts the identity information about the assembly, and then closes the file. This method avoids the memory overhead of having that assembly loaded permanently into the application domain, which is always a consequence of obtaining an Assembly reference: AssemblyName name = AssemblyName.GetAssemblyName(@"c:\CookbookSamples\MyAssembly.exe "); Once you have the AssemblyName reference, you can use various properties and methods to extract specific aspects of the fully qualified name. Listing 19.1 shows how to do so for the currently executing assembly. Listing 19.1 Displaying Information About Assembly Identity// get the AssemblyName reference for the required assembly // in this case the currently executing assembly Assembly execAssembly = Assembly.GetExecutingAssembly(); AssemblyName name = execAssembly.GetName(); // now extract various bits of information Console.WriteLine("Name: " + name.Name); Console.WriteLine("Culture: " + name.CultureInfo.Name); Console.WriteLine("Version: " + name.Version.ToString()); byte [] publicKeyToken = name.GetPublicKeyToken(); if (publicKeyToken == null) Console.WriteLine("This assembly does not have a strong name"); else { Console.Write("Public Key Token: "); foreach (byte b in publicKeyToken) Console.Write(b.ToString() + " "); Console.WriteLine(); } There are several classes at work here, which between them allow a very fine degree of control over the information you can obtain:
If you simply want the fully qualified name of an assembly as a string, and you already have the appropriate Assembly reference, then there is no need to use an AssemblyName class. There's a shortcut via the Assembly.FullName property: Assembly execAssembly = Assembly.Load("mscorlib"); Console.WriteLine(execAssembly.CodeBase); This code, for version 1.1 of the .NET framework, generates the following output: mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 CommentsNote that the assembly identity is sometimes referred to as its fully qualified name. If you want to read the informational attributes of an assembly, or any other information stored in the form of custom attributes, you need to use the System.Attribute class and techniques from reflection. Details of how to do so appear in Chapter 23. |
< Day Day Up > |