我正在使用一个库,该库需要我提供一个实现此接口的对象:
public interface IConsole {
TextWriter StandardInput { get; }
TextReader StandardOutput { get; }
TextReader StandardError { get; }
}
该对象的读者然后被图书馆使用:
IConsole console = new MyConsole();
int readBytes = console.StandardOutput.Read(buffer, 0, buffer.Length);
通常,实现 IConsole 的类具有来自外部进程的 StandardOutput 流。在这种情况下,console.StandardOutput.Read 调用通过阻塞工作,直到有一些数据写入 StandardOutput 流。
我正在尝试做的是创建一个测试IConsole 实现,它使用MemoryStreams 并将StandardInput 上出现的任何内容回显到StandardInput。我试过:
MemoryStream echoOutStream = new MemoryStream();
StandardOutput = new StreamReader(echoOutStream);
但问题在于 console.StandardOutput.Read 将返回 0 而不是阻塞,直到有一些数据。如果没有可用数据或者我可以使用不同的内存流,我是否可以让 MemoryStream 阻塞?