2013-03-12 49 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