Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roslyn/CSharpScript - How to Save/Load Compilation to avoid compilation wait time

I'm trying to integrate c# scripting into my database application. I'm using the globals object to make global variables accessible from the script.

I'm not happy with the wait time if the script is compiled for the first time.
How can I save and load the compilation to avoid the wait time?

Script<object> script = CSharpScript.Create(scriptCode, globalsType: typeof(MyGlobals));
script.Compile();   //<-- load the Compilation from database/file here
script.RunAsync(myGlobalsInstance).Wait();
like image 938
Bogomil Avatar asked Sep 13 '16 09:09

Bogomil


1 Answers

You can create a CSharpScript, then get the compilation via GetCompilation to get the compilation.

var script = CSharpScript.Create("script..");
var compilation = script.GetCompilation();

From the compilation, you can actually compilation.Emit() the dll and pdb to disk or memory. The tricky (and roslyn internal bit) is how to get the delegate of the code to execute, once you have the assembly. You can see how roslyn does it here.

like image 134
m0sa Avatar answered Nov 02 '22 16:11

m0sa