3.5 Load an Assembly into the Current Application Domain


Problem

You need to load an assembly at run time into the current application domain.

Solution

Use the static Load or LoadFrom methods of the System.Reflection.Assembly class.

Discussion

The CLR will automatically load the assemblies identified at build time as being referenced by your assembly. However, you can also explicitly instruct the runtime to load assemblies. The Load and LoadFrom methods both result in the runtime loading an assembly into the current application domain, and both return an Assembly instance that represents the newly loaded assembly. The difference between each method is the arguments that you must provide to identify the assembly to load, and the process that the runtime undertakes to locate the specified assembly.

The Load method provides overloads that allow you to specify the assembly to load using one of the following:

  • A string containing the fully or partially qualified display name of the assembly

  • A System.Reflection.AssemblyName containing details of the assembly

  • A byte array containing the raw bytes that constitute the assembly

Most often, you will use a display name to load an assembly. A fully qualified display name contains the assembly's text name, version, culture, and public key token, separated by commas (for example, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ). To specify an assembly that doesn't have a strong name, use PublicKeyToken=null . You can also specify a partial display name, but as a minimum, you must specify the assembly name (without the file extension). The following code demonstrates various uses of the Load method.

 // Load the System.Data assembly using a fully  // qualified display name. string name1 = "System.Data,Version=1.0.5000.0," +      "Culture=neutral,PublicKeyToken=b77a5c561934e089"; Assembly a1 = Assembly.Load(name1); // Load the System.Xml assembly using an AssemblyName. AssemblyName name2 = new AssemblyName(); name2.Name = "System.Xml"; name2.Version = new Version(1,0,5000,0); name2.CultureInfo = new CultureInfo(""); name2.SetPublicKeyToken(     new byte[] {0xb7,0x7a,0x5c,0x56,0x19,0x34,0xe0,0x89}); Assembly a2 = Assembly.Load(name2); // Load the SomeAssembly assembly using a partial display name Assembly a3 = Assembly.Load("SomeAssembly"); 

In response to the Load call, the runtime undertakes an extensive process to locate and load the specified assembly. Here's a summary; consult the .NET Framework SDK documentation for more details.

  1. If you specify a strong-named assembly, the Load method will apply version policy and publisher policy to enable requests for one version of an assembly to be satisfied by another version. Version policy is specified in your machine or application configuration file using < bindingRedirect > elements. Publisher policy is specified in special resource assemblies installed into the global assembly cache (GAC).

  2. Once the runtime has established the correct version of an assembly to use, it attempts to load strong-named assemblies from the GAC.

  3. If the assembly isn't strong named or isn't found in the GAC, the runtime looks for applicable < codeBase > elements in your machine and application configuration files. A < codeBase > element maps an assembly name to a file or a URL. If the assembly is strong named, < codeBase > can refer to any location including Internet-based URLs; otherwise , < codeBase > must refer to a directory relative to the application directory. If the assembly doesn't exist at the specified location, Load throws a System.IO.FileNotFoundException .

  4. If there are no < codeBase > elements relevant to the requested assembly, the runtime will locate the assembly using probing . Probing looks for the first file with the assembly's name (with either a .dll or .exe extension) in the following locations:

    • the application root directory

    • directories below the application root that match the assembly's name and culture

    • directories below the application root that are specified in the private binpath

The Load method is the easiest way to locate and load assemblies, but can also be expensive in terms of processing if the runtime needs to start probing many directories for a weak-named assembly. The LoadFrom method allows you to load an assembly from a specific location. If the specified file isn't found, the runtime will throw a FileNotFoundException . The runtime won't attempt to locate the assembly in the same way as the Load method ” LoadFrom provides no support for the GAC, policies, < codebase > elements, or probing. This code demonstrates use of the LoadFrom method to load the assembly named c:\shared\MySharedAssembly.dll. Notice that unlike the Load method, LoadFrom requires that you specify the extension of the assembly file.

 // Load the assembly named c:\shared\MySharedAssembly.dll Assembly a4 = Assembly.LoadFrom(@"c:\shared\MySharedAssembly.dll"); 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net