2013-03-12 50 views
-1

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

我把两个richTextEdit放在我的Form中,我想在richTextEdit1中编写php,按下一个按钮,然后在richTextEdit2中查看结果。

PHP代码为:

回声 “你好字!”

按钮的代码是:

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.Out作为输出,它工作正常,但并不有用。如果没有,我没有结果。

任何人都可以帮助我吗?

回答

2

在阅读ms的内容之前,请致电sw.Flush()。我建议在阅读ms之前将sw的用法用到使用语句中,并明确指定编码using (sw = new StreamWriter(ms, Encoding.UTF8))

+0

完美地工作!谢谢 – Redax 2013-03-13 10:05:44