I succeed in serializing instances of the following class but when I try to deserialize right after I get the following error message: " Invalid field in source data: 0"
.
我成功地序列化了下面类的实例,但当我在得到以下错误消息后尝试去反序列化时:“源数据中无效字段:0”。
I have no clue what it refers to because I find below class straight forward. I just updated protobuf-net version to 2.00.614 (runtime version: 2.0.50727).
我不知道它指的是什么,因为我发现下面的课程是直接的。我只是更新了原始版本到2.00.614(运行时版本:2.0.50727)。
Any idea whether I am possibly overlooking something trivial?
你知道我是否可以忽略一些小事吗?
[ProtoContract]
public class TimeSeriesProperties
{
[ProtoMember(1)]
public string TimeSeriesName { get; private set; }
[ProtoMember(2)]
public string FileName { get; private set; }
[ProtoMember(3)]
public string TemplateName { get; private set; }
[ProtoMember(4)]
public int PacketLength { get; private set; }
[ProtoMember(5)]
public long FileSizeBytes { get; set; }
[ProtoMember(6)]
public long NumberRecords { get; set; }
[ProtoMember(7)]
public DateTime DateTimeStart { get; set; }
[ProtoMember(8)]
public DateTime DateTimeEnd { get; set; }
public TimeSeriesProperties()
{
}
public TimeSeriesProperties(string timeSeriesName, string fileName, string templateName, int PacketLength)
{
this.TimeSeriesName = timeSeriesName;
this.FileName = fileName;
this.TemplateName = templateName;
this.PacketLength = PacketLength;
}
}
public static byte[] Serialize_ProtoBuf<T>(T serializeThis)
{
using (var stream = new MemoryStream())
{
ProtoBuf.Serializer.Serialize<T>(stream, serializeThis);
return stream.GetBuffer();
}
}
public static T Deserialize_ProtoBuf<T>(byte[] byteArray)
{
using (var stream = new MemoryStream(byteArray))
{
return ProtoBuf.Serializer.Deserialize<T>(stream);
}
}
1 个解决方案
#1
6
The most common cause I've seen of this is simply incorrect use of GetBuffer()
on a MemoryStream
. That was already my hunch when I added my comment, but you've confirmed it:
我所看到的最常见的原因是在MemoryStream上不正确地使用GetBuffer()。当我添加我的评论时,这已经是我的直觉了,但你已经证实了:
using (var stream = new MemoryStream())
{
ProtoBuf.Serializer.Serialize<T>(stream, serializeThis);
return stream.GetBuffer();
}
GetBuffer
returns the oversized backing-buffer. It has garbage at the end of it. It is perfectly fine to use GetBuffer
, as long as you also record the .Length
, which is the amount of valid data in there. This can avoid an extra allocation of a potentially large array. But in your case, a simpler approach is probably to use ToArray()
to get a right-sized buffer:
GetBuffer返回过大的缓冲缓冲区。它的末端有垃圾。使用GetBuffer非常好,只要您还记录了. length,这就是其中的有效数据量。这可以避免额外分配一个潜在的大数组。但是在您的例子中,更简单的方法可能是使用ToArray()来获得一个合适大小的缓冲区:
using (var stream = new MemoryStream())
{
ProtoBuf.Serializer.Serialize<T>(stream, serializeThis);
return stream.ToArray();
}
#1
6
The most common cause I've seen of this is simply incorrect use of GetBuffer()
on a MemoryStream
. That was already my hunch when I added my comment, but you've confirmed it:
我所看到的最常见的原因是在MemoryStream上不正确地使用GetBuffer()。当我添加我的评论时,这已经是我的直觉了,但你已经证实了:
using (var stream = new MemoryStream())
{
ProtoBuf.Serializer.Serialize<T>(stream, serializeThis);
return stream.GetBuffer();
}
GetBuffer
returns the oversized backing-buffer. It has garbage at the end of it. It is perfectly fine to use GetBuffer
, as long as you also record the .Length
, which is the amount of valid data in there. This can avoid an extra allocation of a potentially large array. But in your case, a simpler approach is probably to use ToArray()
to get a right-sized buffer:
GetBuffer返回过大的缓冲缓冲区。它的末端有垃圾。使用GetBuffer非常好,只要您还记录了. length,这就是其中的有效数据量。这可以避免额外分配一个潜在的大数组。但是在您的例子中,更简单的方法可能是使用ToArray()来获得一个合适大小的缓冲区:
using (var stream = new MemoryStream())
{
ProtoBuf.Serializer.Serialize<T>(stream, serializeThis);
return stream.ToArray();
}