I'm adding VB.Net scripting to a C# application. How can I recompile a script using CS Script? In the C# backend, I add the script code like this:
public static string AddGlobalCode(string code)
{
// Wrap the user's code in some boilerplate
string codeText =
@"Option Strict Off
Imports System
Namespace DXBScript
Public Module globals
" + code + @"
End Module
End Namespace";
try
{
CSScript.LoadCode(codeText);
return "OK";
}
catch (Exception ex)
{
return ex.Message;
}
}
Where code
is some vb.net code. It could be Class
, Sub
, Function
etc. Ex:
Function DoSomething(x)
Return x + x
End Function
Class SomeClass
...
End Class
CallSomeFunction()
The first time I run the AddGlobalCode
function, it compiles ok and I can call the DoSomething
function. However, if I want to modify the script (without re-starting the entire application), say, for example:
Function DoSomething(x)
Return x + x * 2
End Function
If I run AddGlobalCode
again and then execute the DoSomething
function - with the same parameter - I get the original result (x + x
), not the new one (x + x * 2
). If I run the function with a different parameter I get an error:
error BC30562: 'DoSomething' is ambiguous between declarations in Modules 'DXBScript.globals' and 'DXBScript.globals'.
How do I delete-then-add or modify DoSomething
? Preferably, I would delete-or-modify the entire globals
VB module, then add it back - since there will be more than just the function DoSomething
in the script.
This is my first attempt as CS Script (was using MS Script Control).