Microsoft Bond文档的输入和输出流section提供了此示例代码以从文件反序列化:
using (var stream = new FileStream("example.bin", FileMode.Open))
{
var input = new InputStream(stream);
var reader = new CompactBinaryReader<InputStream>(input);
var example = Deserialize<Example>.From(reader);
}
我尝试反向序列化为文件,但没有任何内容写入文件。
using (var stream = new FileStream("example.bin", FileMode.Create))
{
var output = new OutputStream(stream);
var writer = new CompactBinaryWriter<OutputStream>(output);
Serialize.To(writer, example);
}
有什么想法吗?
答案 0 :(得分:2)
看起来OutputStream还没有被刷新到FileStream中。尝试添加对OutputStream.Flush
的显式调用,例如stream example:
using (var stream = new FileStream("example.bin", FileMode.Create))
{
var output = new OutputStream(stream);
var writer = new CompactBinaryWriter<OutputStream>(output);
Serialize.To(writer, example);
output.Flush();
}
设计OutputStream
时我不在身边,因此我无法对导致其未实施IDisposable
的决策流程发表评论。