views:

147

answers:

1

I'm writing .NET On-the-Fly compiler for CLR scripting and want to implement next idea: there is a template file with C# code, I want to read it, create an assembly, load it and amplify source on-the-fly. How can I do that?

+1  A: 

You can do this with CompileAssemblyFromSource. I've done something simliar in the past, where I augmented some scripts with static class wrappers so they could easily be called. You can see it (or steal from it) here.

To do this, just load your template, add your extra amplifications to the code, and compile it in one shot.


If you want to modify an existing assembly, you will need to use something like Mono.Cecil. It allows you to load and modify as assembly at runtime. There is a CodeProject article using Reflexil, a GPL product based on Cecil, which may help you get some ideas...

Reed Copsey
Thanks for link, it's very useful! And so you mean, that only one way is to change source code before compilation, not after?
abatishchev
No. However, you can probably use something like Cecil (http://www.mono-project.com/Cecil) to inject into your assembly. I'll update to include that.
Reed Copsey