我很难用下面的例子来解释行为:
[ProtoContract]
public class Class1Proto
{
[ProtoMember(1)]
public int data1 = 1;
[ProtoMember(2)]
public string data2 = "MYRANDOMSTRING";
}
[ProtoContract]
public class ProtoChunk
{
[ProtoMember(1)]
public List<Class1Proto> arr = new List<Class1Proto>();
public const int PageSize = 4096;
}
用法:
byte[] page = new byte[ProtoChunk.PageSize];
ProtoChunk originalData = new ProtoChunk();
for (int i = 0; i < 100; i++)
{
Class1Proto p = new Class1Proto();
p.data1 = i * 2;
p.data2 = (i * 2).ToString();
originalData.arr.Add(p);
}
using (var memStream = new MemoryStream(page, writable:true))
{
Serializer.SerializeWithLengthPrefix(memStream, originalData, PrefixStyle.Fixed32);
}
using (var memStream = new MemoryStream(page, writable:false))
{
ProtoChunk deserializedData = Serializer.DeserializeWithLengthPrefix<ProtoChunk>(memStream, PrefixStyle.Fixed32);
}
我的期望是
originalData
deserializedData
应该是相同的。他们大多都是
deserializedData.arr[0].data1 == 1 while originalData.arr[0].data1 == 0
originalData.arr[0].data2 and deserializedData.arr[0].data2
(字符串字段)。