Unload CodeDom-compiled assembly
Asked Answered
T

1

10

I have some C# code (let's call it "script") I am compiling at runtime. It uses an interface in my main program that I use to access its functions. Once compiling is done I have CompilerResults.CompiledAssembly in which case I can CreateInstance(Type).

Once I am done using the script I would like to unload completely. From what I understand, I can only do this if I create a separate app domain: Loading DLLs into a separate AppDomain

I had some questions specific to my implementation:

  1. If I have multiple scripts to compile and want to unload them independently, do I have to create separate app domains for each?
  2. What app domain names should I use? Would GUIDs be a good idea? Are there any names I should avoid that may conflict?
  3. If the assembly is in a separate app domain, will it have any issues accessing the interface in the main program? I am currently doing ReferencedAssemblies.Add(typeof(Interface).Assembly.Location) before I compile.
  4. Can I use CompilerParameters GenerateInMemory=true, or do I have to save it somewhere?
Tracee answered 22/9, 2009 at 22:15 Comment(0)
M
2

Answers in order:

  1. Yes, if you want to unload them independently you'll need separate app domains.
  2. Use whatever. It may help you debugging if you can identify it back to script, but that would be more true for the executing Thread.
  3. Not so long as you set the domain setup's base path to your own.

    AppDomainSetup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

  4. No you don't need to save it and it doesn't sound like it would benefit you.

Metic answered 22/9, 2009 at 22:38 Comment(3)
I had trouble getting this to work so I did some more research. It looks like I'll need to do some more work to get it working: devsource.com/c/a/Using-VS/…. Unless there is something new in later versions?Tracee
Looks like I'm in luck. Since I'm using .NET 3.5 I can use the Managed Extensibility Framework. Of course, I have to go learn it now. :)Tracee
Looks like I'm getting in deeper than I wanted/expected. MEF actually can't unload assemblies since it doesn't use a separate AppDomain. MAF (System.AddIn) can, but is much more complex to use. MAF and MEF can be used together. While I really want to be able to unload an assembly, I think this will have to wait at least for version 2.Tracee

© 2022 - 2024 — McMap. All rights reserved.