Sunday 15 August 2010

c# - Create custom AppDomain and add assemblies to it -


How do I create an appdomain, add assemblies for it, then delete that app domain? This is what I did:

  Fixed zero main (string [] args) {string pathToExe = @ "A: \ user \ Tono \ Desktop \ ConsoleApplication1.exe"; AppDomain myDomain = AppDomain.CreateDomain ("MyDomain"); Assembly A = assembly.load (system.info .ReadBalts (Pathtexexe)); MyDomain.Load (a.FullName); // Accidents here! }   

I have also tried:

  myDomain.Load (file .readlibets (pathtoax));   

How can I add an assembly to an appdomain? Once I do this, I can find the method through reflection and then destroy the appdomain

The exception that I get:

file or Could not load assembly 'Can not specify a system file from' ConsoleApplication1, version = 1.0.0.0, culture = neutral, public key token = zero 'or its dependency.

two quick points: <<>>

  • AppDomain.Load loads an assembly in the current appdomain and not on myDomain (weird, I know).
  • AppDomain.Load Load refers to an assembly loaded in the context of the application base-dir, private-bin-path and assemblies from GAC Resolves. Most probably, the assemblies that you try to load are not in any of these places that show an exception message.

    Take a look at this for more information.

    One way to load assemblies in appmail is by creating a MarshallBurf-derived loader. To avoid leak type (and assemblies) from your main AppDomain, you need something like this. Here's a simple way:

      Public class simpleAssistance: Loader: MarshallBirf Object {Public Zero Load (String Path) {Validate Path (Path); Assembly.Load (path); } Public Zero LoadFrom {string path} {valid path (path); Assembly.LoadFrom (path); } Private Zero path (string path) {if (path == empty) Repeat new logic ("Path"); If (! System.IO.File.Exists (path)) leave the new argument exception (string.format ("path \" {0} \ "does not exist", path)); }}   

    and use it like this:

      // Create the loader (a proxy) Var Assembler ({SimpleAssemblyLoader} myDomain.CreateInstanceAndUnwrap (typef ( SimpleAssemblyLoader) .Assembly.FullName, typeof (SimpleAssemblyLoader) .FullName); Load an assembly in the load load context. Note that load reference // will not work unless you set AppDomain base-dir and private-bin-path properly. AssemblyLoader.LoadFrom (pathToExe); //do whatever you want to do. // AppDomain Unload AppDomain.Unload (mydomain) at the end;    

  • No comments:

    Post a Comment