tags:

views:

123

answers:

2

Hello, Consider I have a program running, which should do the following- Should read some code from a text file and should execute it in its own Assembly space so that the new code being executed can access all components of the code already running. How is that possible?? Help please.

A: 

Not sure how the magic works, but I'd look at MyGeneration, which is open source and acheives what you want to do. It can read a file of C#, VB.Net, and JScript and then build/compile those scripts and gives them access to it's own set of classes and assemblies.

MrTelly
+2  A: 

By assembly space, do you mean AppDomain?

You might want to look at IronPython. As a scripting language it is better suited for being dynamically added to the program at runtime.

Otherwise:

You have access to the C# (and VB.NET) compiler via the Microsoft.CSharp.CSharpCodeProvider sitting in System.dll.

You can use it to compile that text file into a separate dll (possibly in-memory). Make sure that you add your currently executing assembly as a reference while compiling. Then you can either

  • load the assembly into the currently executing AppDomain. This way, it has access to all the objects in your application. You, however, cannot unload the code again. You'll have to unload the whole AppDomain (i.e. your application)
  • load the assembly into a separate AppDomain. You'll have to explicitly provide access to some of your objects via Remoting (or other in process communication methods), but you can unload/replace the code in case the text file changes.

Either way, you'll have to use reflection to call your dynamically loaded code.

SealedSun