23.1 Viewing Assembly Information

 <  Day Day Up  >  

You want to load an assembly and ascertain certain properties about it.


Technique

The first step you must perform before viewing anything about an assembly is to load it. Call the LoadFile static method defined in the Assembly class within the System.Reflection namespace. The following example demonstrates loading an assembly from a open file dialog. The examples in this chapter create an assembly-viewer application:

 
 private void mnuOpen_Click(object sender, System.EventArgs e) {     if( openFileDialog1.ShowDialog(this) == DialogResult.OK )     {         try         {             mAssembly = Assembly.LoadFile( openFileDialog1.FileName );             PopulateTree();         }         catch( Exception ex )         {             MessageBox.Show( ex.Message );         }     } } 

The CodeBase and EscapedCodeBase properties are Uniform Resource Identifier (URI) “based paths to the location of the assembly, and the Location property specifies the location using regular file-system notation. You can determine the Common Language Runtime (CLR) version an assembly needs to run by inspecting the ImageRuntimeVersion property.

Even though the Assembly class contains only a few properties, you can see more information by calling the GetName method. It returns an AssemblyName object that you can use to determine information such as the major and minor version as well as the ability to get and set the public key token with strong named assemblies.

Comments

One common theme that you will see as you work with the reflection classes is the use of Get Xxxx methods instead of properties. This design choice remains consistent with method names and their associated parameters throughout the System.Reflection namespace.

The AssemblyName class mentioned earlier plays a vital role within the architecture of the CLR. To avoid file collisions, which occur as a result of installing an older version of a file over a newer version (referred to as DLL Hell ), the AssemblyName class gives identity to the assembly it represents. Rather than use the older methods of simply placing a file in a location, the CLR instead uses the AssemblyName class of an assembly to determine whether it's the correct assembly being requested by an application. The name of an assembly consists of a simple name , which is generally the name of the file, the version number, a cryptographic key pair, and a value denoting culture information. This identity, called the FullName , is used by any applications that reference the assembly, guaranteeing that an older version, which subsequently will have a different version and cryptographic key pair, is not accessed when a newer version is expected and known to be compatible.

 <  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