2

.NET application I work on allows to extend it's functionality with C# scripts that are put into configuration file and compiled like that:

CodeDomProvider provider;
...
var result = provider.CompileAssemblyFromSource(prms, new[] { scriptCode });
var asm = result.CompiledAssembly;
var t = asm.GetType("ScriptClass");
var scriptInstance = (IScript)Activator.CreateInstance();

I have to allow users to change configuration file, catch this event and recompile scripts in runtime without application restart.

I'm warned that previous code will still utilize memory. How should I unload compiled code?

Thank you in advance!

Andrew Florko
  • 7,672
  • 10
  • 60
  • 107

2 Answers2

1

AFAIK, the only way to do this is to load it into a separate AppDomain and kill the entire AppDomain. I imagine this means moving much of the code related to compiling it to the second AppDomain, too. Then the question becomes how to talk cross-AppDomain; there, implicit remoting is IMO the easiest option.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

Since you'll have to use different AppDomains, you should have a look at the System.Addin namespace. It eases extensibility. A full discussion on SO is available here: Choosing between MEF and MAF (System.AddIn)

Community
  • 1
  • 1
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298