1

我有一个用于我正在开发的通信库的测试套件,protobuf-net运行正常。所有测试通过。但是如果我将 PrefixStyle 从 Base128 更改为 Fixed32,反序列化将失败。

我从该TryDeserializeWithLengthPrefix函数收到的异常是:


System.ArgumentNullException was caught
  Message="Value cannot be null.\r\nParameter name: type"
  Source="protobuf-net"
  ParamName="type"

如果我在序列化和反序列化消息时简单地保留 PrefixStyle.Base128,一切都会正常工作。

有谁知道可能会发生什么?

4

1 回答 1

0

Hohum,是的,看起来像一个错误(现已记录);下面的可重复示例。我会看看我能不能在火车上修好它(很快)。对不起'回合:

using System;
using System.IO;
using ProtoBuf;
[ProtoContract]
public class Strange // test entity
{
    [ProtoMember(1)]
    public string Foo { get; set; } // test prop
    [ProtoMember(2)]
    public int Bar { get; set; } // test prop

    static void Main() {
        var original = new Strange { Foo = "abc", Bar = 123 };
        // serialize and deserialize with base-128
        using (MemoryStream ms = new MemoryStream()) {
            Serializer.SerializeWithLengthPrefix(ms, original, PrefixStyle.Base128,1);
            ms.Position = 0;
            object obj;
            Serializer.NonGeneric.TryDeserializeWithLengthPrefix(ms,
                PrefixStyle.Base128, i => typeof(Strange),out obj);
            var clone = (Strange)obj;
            Console.WriteLine("Foo via Base128: " + clone.Foo); // works fine
            Console.WriteLine("Bar via Base128: " + clone.Bar);
        }
        // serialize and deserialize with fixed-32
        using (MemoryStream ms = new MemoryStream())
        {
            Serializer.SerializeWithLengthPrefix(ms, original, PrefixStyle.Fixed32,1);
            ms.Position = 0;
            object obj;
            // BOOM here; oh how embarrassing
            Serializer.NonGeneric.TryDeserializeWithLengthPrefix(ms,
                PrefixStyle.Fixed32, i => typeof(Strange), out obj);
            var clone = (Strange)obj;
            Console.WriteLine("Foo via Fixed32: " + clone.Foo);
            Console.WriteLine("Bar via Fixed32: " + clone.Bar);
        }
    }
}
于 2009-09-25T14:54:58.473 回答