19.10 Reading Assembly Identity Programmatically

 <  Day Day Up  >  

You want to obtain information related to an assembly's identity, including its name , version, culture, and public key.


Technique

Identity 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:

  • AssemblyName.Name returns a string containing the name of the assembly.

  • AssemblyName.CultureInfo returns a System.Globalization.CultureInfo instance, which implements various properties to obtain, for example, the LCID (locale ID), calendar, date-time format, or the language name corresponding to that culture.

  • AssemblyName.Version returns a System.Version instance, which contains properties to return the major, minor, build, and revision numbers of the version and methods to compare versions to see which one has the higher version number.

  • The public-key token is returned as a Byte[] array because it is the appropriate format. This array is returned via a method, AssemblyName.GetPublicKeyToken() , rather than a property. .NET class design guidelines indicate that you should not normally use properties to return arrays. Note that another method, AssemblyName.GetPublicKey() , can return the full public key, also as a byte array.

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 

Comments

Note 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  >  


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