ProtoBuf-net 反序列化不起作用 [英] ProtoBuf-net Deserialize does not work
本文介绍了ProtoBuf-net 反序列化不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码在这里:
var responseMsg = new ResponseMessage()
{
code = ErrorCode.OK,
type = MsgType.LOGIN,
responseStr = "this is local server"
};
var serverStream = new MemoryStream();
ProtoBuf.Serializer.Serialize(serverStream, responseMsg);
Console.WriteLine($"responseMsg {responseMsg?.responseStr ?? "failed"}\n");
var response =ProtoBuf.Serializer.Deserialize<ResponseMessage>(serverStream);
Console.WriteLine($"response {response?.responseStr ?? "failed"}\n");
结果是
responseMsg this is local server
response
ProtoBuf-net 不能反序列化它序列化的内容.这真是一件奇怪的事情
ProtoBuf-net can not Deserialize what it Serialized. it's really a strange thing
推荐答案
您需要通过重置其 Position
才可以读取:
You need to rewind the stream to the beginning by resetting its Position
before you can read from it:
serverStream.Position = 0;
var response = ProtoBuf.Serializer.Deserialize<ResponseMessage>(serverStream);
示例 fiddle.
这篇关于ProtoBuf-net 反序列化不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文