我有一个简单的类象如何序列化/反序列名单<MyClass>使用protobuf网C#
public class Customer{
public long Id;
public string Name;
....
}
我有一个名单,我想使用protobuf网连载了。请指导我简单高效地对此进行序列化和反序列化。
EDIT-1 我对着可用与protobuf网的源代码的单元测试,它使用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
嘿asif,请先阅读[你有什么试用/](http://mattgemmell.com/what-have-you-tried/) – JPVenson
https://code.google.com/p/ protobuf-net/ –