我有一个带有内存流的二进制写入器,如果发生一种情况,我需要清除它:
public void Execute() {
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);
Log log = new Log(writer);
log.Write("OK1");
...
// If condition
memoryStream = new MemoryStream();
...
log.Write("OK2");
...
}
public class Log {
private BinaryWriter log;
public Log(BinaryWriter bw)
{
log = bw;
}
public void write(string msg)
{
log.Write(msg);
}
}
但问题是内存流是空的。我已经尝试再次创建一个新的 BinaryWriter:
public void Execute() {
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);
Log log = new Log(writer);
log.Write("OK1");
...
// If condition
memoryStream = new MemoryStream();
writer = new BinaryWriter(memoryStream);
...
log.Write("OK2");
...
}
public class Log {
private BinaryWriter log;
public Log(BinaryWriter bw)
{
log = bw;
}
public void write(string msg)
{
log.Write(msg);
}
}
但是 BinaryWriter 和 MemoryStream 都是空的。
如何使用内存流清除二进制写入器?我需要将内存流的最终内容写入文件。
谢谢!