C# 序列化集合中的空值会在Protobuf V2.0.0668中引发NullReferenceException 1.可为null的集合中的null值出现NullReference异常。 [协议] 公共类原型测试 { [原成员(1)] 公共列表集合{get;set;} } 静态void Main(字符串[]参数) { ProtoTest测试=新的ProtoTest(); test.Collecton=新列表(); 测试、采集、添加(“A”); test.collection.Add(空); 测试、采集、添加(“B”); test.collection.Add(空); //此代码在Protobuf 1.0.0.282上运行良好 //但在Protobuf 2.0.0.668上引发异常 字节[]缓冲区=序列化(测试); ProtoTest转换=反序列化(缓冲区); //在1.0.0.282中已转换。集合中有2个项被排除在空值之外。 //在2.0.0.668中,序列化失败,出现NullReference异常。 Console.Read(); } 2.尝试在side引用类型集合中的null值上创建默认实例 [协议] 公共抽象类NullableBase { 公共NullableBase() { } [原成员(1)] 公共int?值{get;set;} } [原始合同] 公共类NullableChild:NullableBase { [原成员(2)] 公共字符串StrValue{get;set;} } [原始合同] 公共类原型测试 { [原成员(1)] 公共列表refcollection{get;set;} } 静态void Main(字符串[]参数) { var nullableBaseType=ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(NullableBase),true); AddSubType(100,typeof(NullableChild)); ProtoTest测试=新的ProtoTest(); test.RefCollecton=新列表(); Add(新的NullableChild(){StrValue=“A”}); test.RefCollecton.Add(新的NullableChild(){StrValue=“B”}); test.refcollection.Add(空); 字节[]缓冲区=序列化(测试); //对于引用类型上的null值,Protobuf正在尝试创建默认实例。 //这里的类型是NullBase,它是一个抽象类。Protobuf将无法创建它并引发异常。 //Protobuf为何为空值创建默认实例。 ProtoTest转换=反序列化(缓冲区); Console.Read(); }

C# 序列化集合中的空值会在Protobuf V2.0.0668中引发NullReferenceException 1.可为null的集合中的null值出现NullReference异常。 [协议] 公共类原型测试 { [原成员(1)] 公共列表集合{get;set;} } 静态void Main(字符串[]参数) { ProtoTest测试=新的ProtoTest(); test.Collecton=新列表(); 测试、采集、添加(“A”); test.collection.Add(空); 测试、采集、添加(“B”); test.collection.Add(空); //此代码在Protobuf 1.0.0.282上运行良好 //但在Protobuf 2.0.0.668上引发异常 字节[]缓冲区=序列化(测试); ProtoTest转换=反序列化(缓冲区); //在1.0.0.282中已转换。集合中有2个项被排除在空值之外。 //在2.0.0.668中,序列化失败,出现NullReference异常。 Console.Read(); } 2.尝试在side引用类型集合中的null值上创建默认实例 [协议] 公共抽象类NullableBase { 公共NullableBase() { } [原成员(1)] 公共int?值{get;set;} } [原始合同] 公共类NullableChild:NullableBase { [原成员(2)] 公共字符串StrValue{get;set;} } [原始合同] 公共类原型测试 { [原成员(1)] 公共列表refcollection{get;set;} } 静态void Main(字符串[]参数) { var nullableBaseType=ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(NullableBase),true); AddSubType(100,typeof(NullableChild)); ProtoTest测试=新的ProtoTest(); test.RefCollecton=新列表(); Add(新的NullableChild(){StrValue=“A”}); test.RefCollecton.Add(新的NullableChild(){StrValue=“B”}); test.refcollection.Add(空); 字节[]缓冲区=序列化(测试); //对于引用类型上的null值,Protobuf正在尝试创建默认实例。 //这里的类型是NullBase,它是一个抽象类。Protobuf将无法创建它并引发异常。 //Protobuf为何为空值创建默认实例。 ProtoTest转换=反序列化(缓冲区); Console.Read(); },c#,serialization,protobuf-net,C#,Serialization,Protobuf Net,我们使用Protobuf 1.0.0.282,它在序列化时排除了引用类型和可空集合的空值。但是在v2.0.0.668中,对于可为null的类型和引用类型,它的行为是不同的。对于可为null的类型,它在序列化时对null值抛出NullReferenceException。但对于引用类型,它被序列化,并且在反序列化它的过程中尝试为空值创建默认实例 他们为什么删除此功能?。有什么具体的原因吗?。 若有充分的理由,那个么对于reference和nullable类型集合,它应该抛出nullreferenc

我们使用Protobuf 1.0.0.282,它在序列化时排除了引用类型和可空集合的空值。但是在v2.0.0.668中,对于可为null的类型和引用类型,它的行为是不同的。对于可为null的类型,它在序列化时对null值抛出NullReferenceException。但对于引用类型,它被序列化,并且在反序列化它的过程中尝试为空值创建默认实例

他们为什么删除此功能?。有什么具体的原因吗?。
若有充分的理由,那个么对于reference和nullable类型集合,它应该抛出nullreferenceexception,对吗?我们已经尝试过这种“SupportNull”的解决方案

您使用的是哪个版本?谷歌还是马尔克斯?我看到你在上发表了一篇文章,但你引用了marcs google代码支持论坛的一篇文章。
[ProtoContract]
public class ProtoTest
{
    [ProtoMember(1)]
    public List<string> Collecton { get; set; }
}
static void Main(string[] args)
{
    ProtoTest test = new ProtoTest();
    test.Collecton = new List<string>();
    test.Collecton.Add("A");
    test.Collecton.Add(null);
    test.Collecton.Add("B");
    test.Collecton.Add(null);

    //This code works fine on Protobuf 1.0.0.282
    //But throws exception on Protobuf 2.0.0.668
    byte[] buffer = Serialize<ProtoTest>(test);
    ProtoTest converted = Deserialize<ProtoTest>(buffer);

    //In 1.0.0.282 converted.Collection is having 2 items excluded null values.
    //In 2.0.0.668 Serialization fails with NullReference exception.
    Console.Read();

}
[ProtoContract]        
public abstract class NullableBase
{
    public NullableBase()
    {
    }

    [ProtoMember(1)]
    public int? Value { get; set; }            
}

[ProtoContract]
public class NullableChild:NullableBase
{
    [ProtoMember(2)]
    public string StrValue { get; set; }
}

[ProtoContract]
public class ProtoTest
{
    [ProtoMember(1)]
    public List<NullableBase> RefCollecton { get; set; }

}


static void Main(string[] args)
{
    var nullableBaseType=ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(NullableBase), true);
    nullableBaseType.AddSubType(100, typeof(NullableChild));
    ProtoTest test = new ProtoTest();

    test.RefCollecton = new List<NullableBase>();
    test.RefCollecton.Add(new NullableChild() { StrValue = "A" });
    test.RefCollecton.Add(new NullableChild() { StrValue = "B" });
    test.RefCollecton.Add(null);

    byte[] buffer = Serialize<ProtoTest>(test);
    //For null values on reference type Protobuf is trying to create default instance.
    //Here the type is NullBase and its an abstract class. Protobuf wont be able to create it and throwing exception.
    //Why Protobuf creates default instance for null values.
    ProtoTest converted = Deserialize<ProtoTest>(buffer);
    Console.Read();

}