Background
I'm trying to get the below class to serialize and deserialize a class that implements an abstract class using Protobuf-net. However, it fails when deserializing with the error: "No parameterless constructor found for AbstractTest".
What am I doing incorrectly?
What I Have Tried
I have spent much time researching for an answer, but very few questions involve the abstract nature of my question.
Minimal Example
public class TestRunner
{
public void Go()
{
string encoded = Serialize<ConcreteTest>(new ConcreteTest());
object ob = Deserialize(new System.IO.MemoryStream(Encoding.ASCII.GetBytes(encoded)));
}
public static string Serialize<T>(T data)
{
MemoryStream outputStream = new MemoryStream();
Serializer.Serialize<T>(outputStream, data);
outputStream.Position = 0;
StreamReader outputReader = new StreamReader(outputStream);
return outputReader.ReadToEnd();
}
public static object Deserialize(Stream data)
{
AbstractTest test = Serializer.Deserialize<AbstractTest>(data);
//Error from the line above: No parameterless constructor found for AbstractTest
if (test.ID == 3)
{
return Serializer.Deserialize<ConcreteTest>(data);
}
throw new Exception("We don't know how to handle the message type if I got here!");
}
}
[ProtoBuf.ProtoContract]
[ProtoBuf.ProtoInclude(25, typeof(ConcreteTest))]
public abstract class AbstractTest
{
public AbstractTest()
{
}
[ProtoBuf.ProtoMember(1)]
public int ID = 3;
public abstract void Test();
}
[ProtoBuf.ProtoContract]
public class ConcreteTest : AbstractTest
{
[ProtoBuf.ProtoMember(2)]
public int ID2 = 4;
public override void Test()
{
MasterLog.DebugWriteLine("It worked!");
}
}