How can I compile dynamic code at runtime in the current assembly?

1.2k Views Asked by At

I use VBCodeProvider to compile code, but it generates a new assembly, and also I need to add all references to assemblies I need to use.

Is there a way to compile code in the current assembly?

2

There are 2 best solutions below

6
On BEST ANSWER

You cannot temper the current assembly.

A couple of years ago, I have written an on the topic of dynamic compilation: http://emoreau.com/Entries/Articles/2011/07/Compiling-code-on-the-fly.aspx

BTW, Roslyn is only available if you are using VS2015 (unless you are using the CTPs that were available for VS2013 but it is not a good idea for anything else then testing).

1
On

VBCodeProvider is obsolete. Use Roslyn. But that still will not allow you to amend an existing assembly at runtime because it's not possible.

Compile and load a new assembly. You can use Reflection to automatically add a list of references to a Roslyn compilation.