-1

c# で記述された私の winforms アプリケーションからいくつかの小さなコードを実行する可能性をユーザーに提供したいと考えています。php を使いたいので Phalanger を見つけました。

フォームに 2 つの richTextEdit を配置し、richTextEdit1 に php を記述し、ボタンを押して、richTextEdit2 に結果を表示したいと考えています。

PHPコードは次のとおりです。

echo "こんにちは!";

ボタンコードは次のとおりです。

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 を出力として使用すると、正常に動作しますが、役に立ちません。そうでなければ、私には結果がありません。

誰でも私を助けることができますか?

4

1 に答える 1

2

sw.Flush()ms のコンテンツを読む前に呼び出すだけです。ms を読み込んでエンコーディングを明示的に指定する前に、sw の使用を using ステートメントにラップすることをお勧めします。using (sw = new StreamWriter(ms, Encoding.UTF8))

于 2013-03-13T07:26:59.653 に答える