Вот код:
using System.Diagnostics;
using System.IO;
using ProtoBuf;
namespace ProtoBufTest
{
[ProtoContract]
[ProtoInclude(13, typeof(BuildEvent))]
public abstract class Event
{
[ProtoMember(1)]
public int NodeId { get; set; }
}
[ProtoContract]
public class BuildEvent : Event
{
}
public class Program
{
static void Main(string[] args)
{
var ms = new MemoryStream();
Serializer.SerializeWithLengthPrefix<object>(ms, new BuildEvent(), PrefixStyle.Base128);
Debug.WriteLine(ms.Position);
ms.Position = 0;
var ev = Serializer.DeserializeWithLengthPrefix<BuildEvent>(ms, PrefixStyle.Base128);
Debug.WriteLine(ev.ToString());
}
}
}
Я использую protobuf-net 2.4.0.Запуск этого кода вызывает следующее исключение:
Unhandled Exception: ProtoBuf.ProtoException: No parameterless constructor found for ProtoBufTest.Event
at ProtoBuf.Meta.TypeModel.ThrowCannotCreateInstance(Type type)
at proto_4(Object , ProtoReader )
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read(Object value, ProtoReader source)
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32 key, Object value, ProtoReader source)
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.DeserializeWithLengthPrefix[T](Stream source, PrefixStyle style, Int32 fieldNumber)
at ProtoBuf.Serializer.DeserializeWithLengthPrefix[T](Stream source, PrefixStyle style)
at ProtoBufTest.Program.Main(String[] args) in C:\Work\ProtoBufTest\ProtoBufTest\Program.cs:line 30