我得到一个ProtoException
ProtoBuf.ProtoException : Internal error; a key mismatch occurred
使用以下代码:
[ProtoContract]
class Foo { }
class MemberRemovedTest
{
[ProtoContract]
class V1
{
[ProtoMember(1, AsReference = true)]
public Foo A { get; set; }
[ProtoMember(2, AsReference = true)]
public Foo B { get; set; }
}
[ProtoContract]
class V2
{
[ProtoMember(2, AsReference = true)]
public Foo B { get; set; }
}
public void BasicTest()
{
var v1 = new V1();
v1.A = new Foo();
v1.B = new Foo();
byte[] buffer;
V2 v2;
using (var stream = new MemoryStream())
{
Serializer.Serialize(stream, A);
buffer = stream.ToArray();
}
using (var stream = new MemoryStream(buffer))
{
v2 = Serializer.Deserialize<V2>(stream); //Exception here
}
}
}
如果出现以下情况,它不会抛出异常:
- 第二个成员是被删除的成员。
- or are not的
ProtoMember
属性。A
B
AsReference = true
A
并且B
都不是用Foo
实例设置的。
我理解 protobuf 支持成员删除,但这似乎表明在某些情况下必须保留它们。
这是 Protobuf 中的错误还是关于删除成员的错误假设?
异常调用堆栈:
at ProtoBuf.NetObjectCache.SetKeyedObject(Int32 key, Object value) in c:\Dev\protobuf-net\protobuf-net\NetObjectCache.cs: line 67
at ProtoBuf.BclHelpers.ReadNetObject(Object value, ProtoReader source, Int32 key, Type type, NetObjectOptions options) in c:\Dev\protobuf-net\protobuf-net\BclHelpers.cs: line 425
at proto_6(Object, ProtoReader)
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read(Object value, ProtoReader source) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs: line 57
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32 key, Object value, ProtoReader source) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs: line 715
at ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoReader reader, Type type, Object value, Boolean noAutoCreate) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs: line 679
at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type, SerializationContext context) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs: line 580
at ProtoBuf.Serializer.Deserialize(Stream source) in c:\Dev\protobuf-net\protobuf-net\Serializer.cs: line 77
at ####.ProtoBuf.MemberRemovedTest.BasicTest() in MemberRemovedTest.cs: line 56