我有一個簡單的類象如何序列化/反序列名單<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/ –