asp net 3.5unleashed

Page 465

If no matching assembly is found, the CLR looks in the GAC using the portions of the assembly’s identity specified by the caller. If the culture or public key information wasn’t specified, the behavior of LoadWithPartialName is undefined and you’re not guaranteed to load any particular assembly. However, if only the version wasn’t specified, LoadWithPartialName loads the assembly from GAC that has the highest version number. Important Some developers notice that System.AppDomain offers a Load method. Unlike Assembly’s static Load method, AppDomain’s Load method is an instance method that allows you to load an assembly into the specified AppDomain. This method was designed to be called by unmanaged code, and it allows a host to inject an assembly into a specific AppDomain. Managed code developers generally shouldn’t call this method. Here’s why. When AppDomain’s Load method is called, you pass it a string that identifies an assembly. The method then applies policy and searches the normal places—the user’s disk or codebase references—looking for the assembly. Recall that an AppDomain has settings associated with it that tell the CLR how to look for assemblies. To load this assembly, the CLR will use the settings associated with the specified AppDomain, not the calling AppDomain. However, AppDomain’s Load method returns a reference to an assembly. Because the System.Assembly class isn’t derived from System.MarshalByRefObject, the assembly object must be marshaled by value back to the calling AppDomain. But the CLR will now use the calling AppDomain’s settings to locate the assembly and load it. If the assembly can’t be found using the calling AppDomain’s policy and search locations, a FileNotFoundException exception is thrown. This behavior is usually undesirable and is the reason you should avoid AppDomain’s Load method.

Loading Assemblies as “Data Files” Imagine a user has installed some utility application on her hard drive. The utility consists of two assemblies: SomeTool.exe (the main application’s assembly file) and Component.dll (an assembly file containing some components that SomeTool.exe uses). These two assemblies are installed as follows: C:\SomeTool\SomeTool.exe C:\SomeTool\Component.dll Let’s further suppose that the user has an assembly that she wants to process using SomeTool.exe. The assembly file to be processed happens to be named Component.dll and resides in the following location: C:\Component.dll Now, let’s say that the user executes the following command line: C:\>C:\SomeTool\SomeTool.exe C:\Component.dll SomeTool.exe’s Main method starts running and calls Assembly.LoadFrom, passing the command-line argument indicating the assembly that the tool is to process. The CLR loads the C:\Component.dll assembly. Now let’s say that Main calls some other method that


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.