【发布时间】:2014-02-15 07:12:57
【问题描述】:
对于我当前的项目,我正在使用 protobuf-net,但得到了一些奇怪的结果。
我有一个包含两个属性的类:
[ProtoContract]
class MyClass{
[ProtoMember(1)]
public int ID { get; set; }
[ProtoMember(2)]
public string Description { get; set; }
}
如果我将ID 设置为0,然后输入一个随机的Description,然后检查序列化的byte[],我不会得到预期的结果。
我想用长度前缀序列化类。
我正在像这样对类进行序列化:
var myclass = new MyClass()
{
ID = 0, Description = "ThisIsAShortDescription"
};
using(var stream = new MemoryStream())
{
Serializer.SerializeWithLengthPrefix<MyClass>(stream, myclass, PrefixStyle.Base128);
Console.WriteLine(BitConverter.ToString(stream.GetBuffer());
}
我期待这样的事情:
19 00 17 54 68 69 73 49 73 41 53 68 6f 72 74 44 65 73 63 72 69 70 74 69 6f 6e
19 -> length of the packet
00 -> ID
17 -> length of following string
rest -> string
相反,我得到: 19 12 17 54 68 69 73 49 73 41 53 68 6f 72 74 44 65 73 63 72 69 70 74 69 6f 6e
开头的这个0x12是什么? 0x00 != 0x12? 我认为我所做的一切都是正确的,但我的代码中可能存在愚蠢的错误吗?
【问题讨论】:
标签: c# serialization protocol-buffers protobuf-net