【发布时间】:2021-01-14 05:51:43
【问题描述】:
我目前正在尝试使用 protobuf-net (v3.0.29) 并尝试序列化/反序列化一些简单的类。从最小的工作示例中,我不明白为什么 protobuf-net 将简单的 List<int> Value 序列化/反序列化为没有元素的列表。
public enum BigEnum { One, Two }
[ProtoContract]
public class Container
{
[ProtoMember(1)]
public BigEnum Parameter { get; private set; }
[ProtoMember(2)]
public List<int> Value { get; set; }
public Container()
{
Parameter = BigEnum.One;
Value = new List<int>();
}
}
static class Program
{
static void Main(string[] args)
{
Container data = new Container();
data.Value.Add(-1);
data.Value.Add(1);
MemoryStream memStream = new MemoryStream();
Serializer.Serialize<Container>(memStream, data);
var result = Serializer.Deserialize<Container>(memStream);
}
}
这个 protobuf 文件是由 Serializer.GetProto() 生成的,对我来说看起来不错。
我们如何正确处理List<int> Value 并且 protobuf-net 可以处理更复杂的结构,例如
List<KeyValuePair<String, String>> 或 Dictionary<int,double> 在班级内?
protobuf-net 有一些非常古老的帖子,里面有字典,但我不知道目前的状态是什么。
syntax = "proto3";
package NetCoreProtobuf;
enum BigEnum {
One = 0;
Two = 1;
}
message Container {
BigEnum Parameter = 1;
repeated int32 Value = 2 [packed = false];
}
【问题讨论】:
标签: c# protocol-buffers protobuf-net