C# gRPC Message 序列化与反序列化
原理:通过 Google.Protobuf 的 MessageParser 对 gRpc 的 message 进行解析
示例如下(PllClockInfo 为 xxx.proto 文件中自定义的 message):
using Google.Protobuf; PllClockInfo pllClockInfo1 = (PllClockInfo)request; byte[] bytes = pllClockInfo1.ToByteArray(); MessageParser<PllClockInfo> parser_PllClockInfo = new MessageParser<PllClockInfo>(() => new PllClockInfo()); PllClockInfo pllClockInfo2 = parser_PllClockInfo.ParseFrom(bytes);
注:gRPC Message 所有值都为默认值时,xxx.ToByteArray() 返回空数组。
参考文档:https://developers.google.cn/protocol-buffers/docs/reference/csharp/class/google/protobuf/message-parser-t-?hl=zh-tw#messageparser