0

我正在 silverlight 4 和 WinForms (net4) 中构建 IronRuby 控制台。我可以毫无问题地重定向输出:

MyRuntime = Ruby.CreateRuntime();
msOutput = new MemoryStream();
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8);
MyEngine = MyRuntime.GetEngine("rb");
MySource = MyEngine.CreateScriptSourceFromString("a='123'\nputs a", SourceCodeKind.Statements);
MySource.Execute();
textBox2.Text = ReadFromStream(msOutput);

现在,我也想重定向输入,但总是从脚本中得到一个“nil”:

MyRuntime = Ruby.CreateRuntime();
msOutput = new MemoryStream();
msInput = new MemoryStream();
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8);
MyRuntime.IO.SetInput(msInput, Encoding.UTF8);
MyEngine = MyRuntime.GetEngine("rb");
MySource = MyEngine.CreateScriptSourceFromString("a=gets\nputs a", SourceCodeKind.Statements);
byte[] byteArray = Encoding.UTF8.GetBytes("123");
msInput.Write(byteArray, 0, byteArray.Length);
MySource.Execute();
textBox2.Text = ReadFromStream(msOutput);

我找不到任何重定向输入的样本,你能发一个例子吗?谢谢你。

4

1 回答 1

1

我没有立即可用的任何示例代码,但您需要实现流而不是使用 MemoryStream。当对流进行读取时,您需要将文本框的“内容”发送到流。您将需要一些机制来确定何时发送内容 - 例如,当用户点击返回时。您可能还需要设置一个线程来阻止读取,并可能使用 AutoResetEvent 来阻止,直到文本框发出输入完成的信号。

于 2010-10-06T05:16:12.403 回答