字节数组上的protobuf-net OverwriteList [英] protobuf-net OverwriteList on Byte Array
问题描述
我正在尝试通过protobuf-net发送IPEndpoint,而我观察到的是,当将4个字节的数组反序列化为IP4地址时,设置的代码接收到8个字节的值.四个字节包含原始地址,另外四个字节包含序列化的地址.通过逐步检查代码,我可以确认调用Deserialize时,它首先读取字节,然后将其设置为字节.
What I am doing is attempting to send an IPEndpoint through protobuf-net and what I observed is that when deserializing the array of 4 bytes into the IP4 address, the set code recieves a value of 8 bytes. Four bytes containing the orignal address, and 4 more bytes containing the address that was serialized. By stepping through the code I have been able to confirm that when Deserialize is called, it first reads the bytes, and then sets they bytes.
经过阅读后,我了解了OverwriteList,并且如下面的示例所示,我将其设置为true.但是,设置程序仍会得到一个8字节的值.
After doing some reading I learned about OverwriteList, and as can been seen in the example below, I have set that to true. However the setter is still provided an 8 byte value.
有人知道我在做什么错吗?
Does anyone have a clue what I am doing wrong?
当与protobuf-net r480,Visual Studio 2010作为.Net 4.0控制台应用程序一起使用时,此示例代码应引发异常.
This sample code should throw an exception when used with protobuf-net r480, Visual Studio 2010 as a .Net 4.0 console application.
using ProtoBuf;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
[ProtoContract]
class AddressOWner
{
private IPEndPoint endpoint;
public AddressOWner()
{ endpoint = new IPEndPoint(new IPAddress(new byte[] {8,8,8,8}), 0); }
public AddressOWner(IPEndPoint newendpoint)
{ this.endpoint = newendpoint; }
[ProtoMember(1, OverwriteList=true)]
public byte[] AddressBytes
{
get { return endpoint.Address.GetAddressBytes(); }
set { endpoint.Address = new IPAddress(value); }
}
}
class Program
{
static void Main(string[] args)
{
AddressOWner ao = new AddressOWner(new IPEndPoint(new IPAddress(new byte[] { 192, 168, 1, 1 }), 80));
MemoryStream ms = new MemoryStream();
Serializer.Serialize(ms, ao);
byte[] messageData = ms.GetBuffer();
ms = new MemoryStream(messageData);
AddressOWner aoCopy = Serializer.Deserialize<AddressOWner>(ms);
}
}
}
推荐答案
看来这实际上是一个错误,特定于 byte []
,该错误被视为特定的protobuf原语.其他数组/列表被映射到 repeated
(以protobuf术语),并正确处理 OverwriteList
选项.我将调整 byte []
处理以支持此选项.
It looks like this is actually a bug, specific to byte[]
, which is handled as a particular protobuf primitive. Other arrays/lists are mapped to repeated
(in protobuf terms), and handle the OverwriteList
option correctly. I will tweak the byte[]
handling to support this option.
r484已修复此问题,并支持集成测试
this is fixed in r484, with supporting integration test
这篇关于字节数组上的protobuf-net OverwriteList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!