How do you integrate the C# Scripting API (csi.exe) with a host program?

吃可爱长大的小学妹 提交于 2020-06-26 12:40:31

问题


For the sake of simplicity, if I have a game engine. Use C# compiled. How would I call a C# script using the csi.exe (or some other method). How do I use the C# Scripting API in another program like Lua?

Edit: Clarification. I do not want to call C#'s csi.exe from Lua. I want to call the csi.exe from a compiled program, whether that is C# (a compiled C# program), C++, or something else. I did not mean it to sound like calling the Scripting API from Lua. I mean like other programs use Lua. That is how I want to use the C# Scripting API. I am mostly interested in how to call the C# script from a compiled C# program.


回答1:


You don't have to use csi.exe, you can actually compile and execute C# within your application (with context to your solution objects, classes and methods).

I haven't found some real documentation about it beside some blog posts.

The main methods you should look at are in Microsoft.CodeAnalysis.CSharp.Scripting and Microsoft.CodeAnalysis.Scripting.

Look over CSharpScript.RunAsync and CSharpScript.EvaluteAsync, these two would compile and execute a given C# script, with syntax similar to the csi.exe syntax.

For example if you wish the user to enter some code and expect this code to return some string while exposing the user to the MyClassassembly:

private async void CompileAndExecuteLine(string userCode)
    {
        string output = "";
        ScriptOptions scriptOptions = ScriptOptions.Default.WithReferences(typeof(MyClass).Assembly);

        try
        {
            output = await CSharpScript.EvaluateAsync<string>(userCode, scriptOptions);
        }
        catch (CompilationErrorException cee)
        {
            string message = "You got errors:" + "\r\n";
            foreach (Diagnostic dia in cee.Diagnostics)
            {
                message += dia.ToString() + "\r\n";
            }
            MessageBox.Show(message, "Compilation Error");
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }

        return output;
    }

In this example the code you enter must return a string (need to end with return someString;), you can call any class and method from the referenced assembly, and even declare your own classes within the userCode (but no namespaces).

If you wish to have some terminal environment (=always return some string), consider to manipulate the user defined code before execution.




回答2:


To call into csi.exe from lua, you would use

os.execute("c:\\<csi.exe location>\csi.exe myscript.csx")

If you need to pass input arguments then

os.execute("c:\\<csi.exe location>\csi.exe myscript.csx myArg")

Inside your myscript.csx file you can access the arguments via

Args[0] ... Args[n]


来源:https://stackoverflow.com/questions/39775568/how-do-you-integrate-the-c-sharp-scripting-api-csi-exe-with-a-host-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!