< Day Day Up > |
TechniqueThe Assembly class is not remotable. In general, it is not possible to manipulate an assembly in a remote application domain. Loading an assembly with some method such as Assembly.Load() generally loads that assembly into the domain that is currently executing. If you want an assembly loaded into a new application domain, you need to explicitly create that domain and then have that domain itself execute the code that loads the assembly. Listing 19.2 illustrates a Main() method that loads an assembly with the path specified by the first argument so you can examine that assembly's fully qualified name . Listing 19.2 Main() Method That Loads Another Assemblystatic void Main(string[] args) { Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + ": " + args[0]); Assembly assembly = Assembly.LoadFrom(args[0]); Console.WriteLine(assembly.FullName); } This code loads the assembly onto the current application domain. To execute this code in a separate domain, let's assume the assembly containing this code is stored in the file C:\CookbookSamples\AssemblyLoader.exe . Listing 19.3 shows how you would execute Listing 19.2 in a new application domain to examine System.Windows.Forms.dll . Listing 19.3 Executing Code in a New Application Domainstring path = @"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Windows.Forms.dll"; AppDomain childDomain = AppDomain.CreateDomain("child domain"); childDomain.ExecuteAssembly(@"C:\CookbookSamples\AssemblyLoader.exe ", null, new string[]{path} ); AppDomain.Unload(childDomain); This code shows three steps:
CommentsOne reason for loading an assembly into a separate application domain is to allow for unloading: The CLR does not at present supply any means to unload an individual assembly, but it is possible to unload an application domain. This step might be important, for example, if your application needs to load a large number of assembliesperhaps to examine information about those assemblies. Once it does so, it no longer needs each assembly. If those assemblies remain loaded, it could impact performance by increasing the application's virtual-memory requirements. |
< Day Day Up > |