Reputation: 6335
I am trying to upgrade dependencies of a legacy code base to use protobuf-net 2.3.7 instead of protobuf-net 1.0
The following code used to work with version 1 and prints 2147483647
var stream = new MemoryStream();
Serializer.NonGeneric.SerializeWithLengthPrefix(stream, int.MaxValue, PrefixStyle.Base128, 1);
stream.Position = 0;
Serializer.NonGeneric.TryDeserializeWithLengthPrefix(stream, PrefixStyle.Base128, _ => typeof(int).MakeByRefType(), out var lastItem);
Console.WriteLine(lastItem);
But the same code doesn't work with protobuf-net 2.3.7 and throws:
Unhandled Exception: System.InvalidOperationException: Type is not expected, and no contract can be inferred: System.Int32& at ProtoBuf.Meta.TypeModel.ThrowUnexpectedType(Type type) at ProtoBuf.Meta.TypeModel.TryDeserializeAuxiliaryType(ProtoReader reader, DataFormat format, Int32 tag, Type type, Object& value, Boolean skipOtherFields, Boolean asListItem, Boolean autoCreate, Boolean insideList, Object parentListOrType) at ProtoBuf.Meta.TypeModel.DeserializeWithLengthPrefix(Stream source, Object value, Type type, PrefixStyle style, Int32 expectedField, TypeResolver resolver, Int64& bytesRead, Boolean& haveObject, SerializationContext context) at ProtoBuf.Serializer.NonGeneric.TryDeserializeWithLengthPrefix(Stream source, PrefixStyle style, TypeResolver resolver, Object& value)
Although the exception makes sense I am trying to find a way to make the code to work with the new version of the library without doing a massive refactoring.
Any suggestions?
EDIT:
MemoryStream
contains 8 bytes after serialization:
new byte[] {10,6,8,255,255,255,255,7}
base64: CgYI/////wc=
Upvotes: 1
Views: 423
Reputation: 1064324
That has never been a specifically designed scenario, and frankly I'm amazed that it ever worked, even in 1.something, but: this seems to work:
Serializer.NonGeneric.TryDeserializeWithLengthPrefix(stream, PrefixStyle.Base128, _ => typeof(int), out var lastItem);
Console.WriteLine(lastItem); // lastItem is a boxed int
Is there a specific reason you want a wrapped pointer?
Upvotes: 1