0

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).

001
  • 13,291
  • 5
  • 35
  • 66
  • Just glancing at the docs - have you tried `CSScript.Evaluator.LoadCode()`. I guess [Choosing Compiler Engine](https://github.com/oleg-shilo/cs-script/wiki/Choosing-Compiler-Engine) also my hold some clues. – Filburt Feb 23 '21 at 20:28
  • @Filburt I just tried changing to `CSScript.Evaluator.LoadCode(codeText, null);` and have the same issues. Elsewhere I had previously set the engine to `CSScript.EvaluatorConfig.Engine = EvaluatorEngine.CodeDom;` – 001 Feb 23 '21 at 20:35
  • Heading *Script Unloading* on [Hosted Script Execution](https://github.com/oleg-shilo/cs-script/wiki/Hosted-Script-Execution) describes the limitations that .NET has here. `UnloadOwnerDomain()` of a RemoteAppDomain seems also worth looking into. – Filburt Feb 24 '21 at 08:29
  • @Filburt Thanks. I have tried that (`LoadCodeRemotely`) but get a new error `A namespace cannot directly contain members such as fields or methods`. I am now thinking this has something to do with the fact that I am using VB and CS Script seems more geared to C#. I will keep investigating. – 001 Feb 24 '21 at 18:14

0 Answers0