Multiple CompileClass calls with a single CSharpScriptExecution instance?
jfcardinal opened this issue · 1 comments
Let's say I have two different classes that I will compile with CompileClass(). Can I compile them using one instance of CSharpScriptExecution, or do I need a new instance of CSharpScriptExecution for each class?
When I tried to use a single instance of CSharpScriptExecution, the second call to CompileClass() returned an instance of the first class, not the second.
Before each call to CompileClass(), I set the GeneratedClassName to a new, unique name.
I think you should use a new instance - think of each instance of a specific instance of code that you're executing.
You can make this work by clearing the .ObjectInstance
prior to executing the second time:
// this enables excecution of the new code or DisableObjectCaching = true
script.ObjectInstance = null;
// dynamic required since host doesn't know about this new type
gen = script.CompileClass(code); // second bit of code now works
I've also added a .DisableObjectCaching
flag which is false by default. You can set that to not cache instances so that code always creates a new instance.
This just rams home the point that it makes sense to use new instances as each instance can maintain it's own generated code units so if you have two bits of code, you can work with either of them individually.
var script = new CSharpScriptExecution()
{
SaveGeneratedCode = true,
AllowReferencesInCode = true,
DisableObjectCaching = true // this enables
};