我正在使用带有 .Net20 版本的 protobuf-net r480 的 Visual Studio 2005。
我尝试遵循一个示例并使用字符串、枚举、int 和字节 [] 数据序列化一个类,如下所示
[ProtoContract]
public class Proto_DemoMessage {
public enum ESSCommandType : int {
ClientServer = 0,
Broadcast = 10,
Assign = 11,
}
[ProtoMember(1, IsRequired = false)]
public string Name;
[ProtoMember(3, IsRequired = true)]
public ESSCommandType CommandType;
[ProtoMember(4, IsRequired = false)]
public int Code;
[ProtoMember(5, IsRequired = false)]
public byte[] BytesData;
public byte[] Serialize(){
byte[] b = null;
using (MemoryStream ms = new MemoryStream()) {
Serializer.Serialize<Proto_DemoMessage>(ms, this);
b = new byte[ms.Position];
byte[] fullB = ms.GetBuffer();
Array.Copy(fullB, b, b.Length);
}
return b;
}
并为每个字段赋值,如下所示
Proto_DemoMessage inner_message = new Proto_DemoMessage();
inner_message.Name = "innerName";
inner_message.CommandType = Proto_DemoMessage.ESSCommandType.Broadcast;
inner_message.Code = 11;
inner_message.BytesData = System.Text.Encoding.Unicode.GetBytes("1234567890");
调用 inner_message.Serialize() 后,我将结果 byte[] 写入文件。当我以 HEX 模式打开文件进行验证时,我发现 byte[] 中的每个字节后面都有一个 00 填充。结果是:
2A 14 31 00 32 00 33 00 34 00 35 00 36 00 37 00 38 00 39 00 30 00
是不是我做错了什么?感谢您的帮助。