在winforms c#中进行Phalanger代码评估


Phalanger code evaluation in winforms C#

我想让用户有可能从我的winforms应用程序中执行一些用c#编写的小代码。我想用php,所以我找到了Phalanger。

我把两个richTextEdit在我的形式,我想写php在richTextEdit1,按下一个按钮,并在richTextEdit2中查看结果。

php代码是:

echo "Hello word ";

按钮代码为:

ScriptContext context = ScriptContext.CurrentContext;
MemoryStream ms;
StreamWriter sw;
ms = new MemoryStream();
sw = new StreamWriter(ms);
context.Output = sw ;
//context.Output = Console.Out;
var res = DynamicCode.Eval(
            richTextBox1.Text,
            false,/*phalanger internal stuff*/
            context,
            null,/*local variables*/
            null,/*reference to "$this"*/
            null,/*current class context*/
            "Default.aspx.cs",/*file name, used for debug and cache key*/
            1, 1,/*position in the file used for debug and cache key*/
            -1,/*something internal*/
            null/*current namespace, used in CLR mode*/
        );
richTextBox2.Text = Encoding.UTF8.GetString(ms.ToArray());

如果我使用Console。作为输出,它工作得很好,但不是有用的。如果没有,我没有结果。

有人能帮我吗?

在读取ms的内容之前,只需调用sw.Flush()。我建议在读取ms之前将sw的用法包装到using语句中,并显式指定编码using (sw = new StreamWriter(ms, Encoding.UTF8))