19.9 Explicitly Loading an Assembly Programmatically

 <  Day Day Up  >  

You want to explicitly load an assembly from another running application. You might want to manipulate the assembly programmatically (for example, to query the information in the manifest or to use reflection to run code in the assembly).


Technique

You explicitly load an assembly using the static Assembly.Load() and Assembly.LoadFrom() methods. These methods load the indicated assembly and return an Assembly reference to that assembly. If the assembly is already loaded, then they simply return the Assembly reference to the preloaded assembly.

Note that the Assembly class is defined in the System.Reflection namespace:

 
 using System.Reflection; 

If you want to load an assembly where you know the name or identity of that assembly, then you should use Assembly.Load() . For example, if you have an assembly with the name MyAssembly , you use the following:

 
 Assembly loadedAssembly = Assembly.Load("MyAssembly"); 

However, supplying merely the name of an assembly is only sufficient if the assembly you are trying to load is located in the same folder as the executing assembly (or, depending on the probing rules for that assembly, in a subfolder). If you want to load a shared assembly from the GAC, you need to supply the full identity of that assembly. For example, to load System.Drawing.dll , you use

 
 Assembly loadedAssembly = Assembly.Load("System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); 

You use Assembly.LoadFrom() instead if you want to load an assembly where you know the name and location of the file that contains the manifest:

 
 Assembly loadedAssembly = Assembly.LoadFrom(@"C:\MyAssemblies\MyAssembly.exe "); 

If you want to explicitly load one of the assemblies supplied in the .NET Framework class library, then on developer machines that have VS.NET installed, you can supply a path that leads to copies of these assemblies that are installed outside the assembly cache in a subfolder of %WINDIR%\Microsoft.NET\Framework for the benefit of VS.NET. For example, to load the .NET 1.1 version of System.Drawing.dll , you use the following:

 
 Assembly loadedAssembly = Assembly.LoadFrom     (@"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Drawing.dll"); 

This technique has the advantage that you don't need to know the full identity of the assembly, but it might not work on user machines on which only the .NET runtime is installed. However, there is a workaround: You can use Assembly.LoadFrom() to load the assembly on your developer machine and then use the Assembly.FullName property to obtain the identity of the assembly:

 
 Assembly loadedAssembly =  Assembly.LoadFrom     (@"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Drawing.dll"); Console.WriteLine(loadedAssembly.FullName); 

The FullName property returns a string representation of the assembly identity in exactly the format required by Assembly.Load() ”so you can cut and paste this string into the code that is to be compiled and shipped.

Note that we present here the simplest overloads of Assembly.Load() and Assembly.LoadFrom() . Other overloads of these methods allow you to supply evidence that determines the security permissions for the assembly or to supply the name in different formats (such as an instance of the System.Reflection.AssemblyName class). In addition, version 1.1 of the .NET Framework brings a new method, Assembly.LoadFile() , which is similar in its effect to Assembly.LoadFrom() :

 
 Assembly loadedAssembly = Assembly.LoadFile(@"C:\MyAssemblies\MyAssembly.exe "); 

Comments

In general, assemblies are loaded automatically as they are needed. For example, the Color struct is contained in System.Drawing.dll . Suppose your code tries to instantiate a Color instance directly (that is, without using reflection):

 
 Color color = Color.Red; 

The compiler of course refuses to compile this code unless your project references System.Drawing.dll , in which case the emitted assembly will contain a reference to System.Drawing.dll in the manifest. The CLR will, on executing the preceding code that instantiates the color variable, automatically load System.Drawing.dll if that assembly hasn't previously been loaded. However, when assemblies are automatically loaded on demand in this way, your code doesn't get any explicit access to the assembly. The advantage of using Assembly.LoadFrom() or Assembly.Load() is that these methods give you a System.Reflection.Assembly reference. The Assembly class provides access to explicitly manipulate an assembly and also provides the gateway to the part of .NET's reflection technology that deals with assemblies. We don't demonstrate many applications of the Assembly reference in this chapter because that's really the topic of reflection, so it is discussed in Chapter 23, "Reflection."

In general, it is not possible to obtain an Assembly reference without loading the assembly ”because it's not possible to manipulate an assembly without loading it first. However, it's worth pointing out that there are simpler ways of obtaining this Assembly reference if the assembly in question is already loaded.

If you want to obtain a reference to the assembly which contains the code that is currently executing, you can use the static Assembly.GetExecutingAssembly() method:

 
 Assembly execAssembly = Assembly.GetExecutingAssembly(); 

You can also use the static method, Assembly.GetEntryAssembly() to get an assembly reference to the .exe assembly that the current process started off executing. With this technique, code in a library can obtain a reference to the ultimate caller:

 
 Assembly entryAssembly = Assembly.GetEntryAssembly(); 

If you want to access the assembly that defines a particular type which is already loaded, then you can use the Type.Assembly property. For example, to obtain an assembly reference to the assembly that defines the SortedList type (this assembly happens to be mscorlib.dll ), you use

 
 Assembly assblyForSortedList = typeof(System.Collections.SortedList).Assembly; 

Alternatively, if you have some object reference, you use the following:

 
 // assume myObj is a variable which can be declared to be of any type Type objType = myObj.GetType(); Assembly assblyForSortedList = objType.Assembly; 
 <  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