【发布时间】:2014-04-11 12:35:59
【问题描述】:
我有一个简单的类,比如
public class Customer{
public long Id;
public string Name;
....
}
我有一个列表,我想使用 protobuf-net 对其进行序列化。请指导我简单有效的序列化和反序列化方法。
EDIT-1 我正在查看 protobuf-net 源代码提供的单元测试,它使用 2 种方式进行序列化,使用反射和模型(内部处理基于 ToString 的映射)。
我从源代码中遵循的是,我使用了与在源代码的项目文件夹 e-ProtoBufNetWithModel 中测试的相同的技术,并创建了一个 TypeModel...
public static TypeModel CreateModel()
{
RuntimeTypeModel model = TypeModel.Create();
model.Add(typeof(Customer), false)
.Add(1, "Id")
.Add(1, "Name");
TypeModel compiled = model.Compile();
return compiled;
}
问题领域
public static Stream SerializeUsingProtobuf(IList<Customer> pSource)
{
var model = CreateModel();
using (Stream memoryStream = new MemoryStream())
{
model.Serialize(memoryStream, pSource);
return memoryStream;
}
}
在TypeModel compiled = model.Compile(); 上,它引发异常
检测到重复的字段编号; 1
【问题讨论】:
标签: c# protobuf-net