I'm trying to serialize an immutable message that has an immutable base class. However, I'm unable to convince protobuf-serializer to deserialize it to a derived type. In short this test gives me an invalid cast exception (from Base to Derived):
[TestFixture]
public class InheritanceTest
{
public class Base
{
public int IntField { get; private set; }
public Base(int intField)
{
IntField = intField;
}
}
public class Derived : Base
{
public string StringField { get; private set; }
public Derived(int intField, string stringField) : base(intField)
{
StringField = stringField;
}
}
[Test]
public void TestInheritance()
{
var serializer = TypeModel.Create();
serializer.Add(typeof (Base), true)
.Add(1, "IntField")
.AddSubType(2, typeof (Derived))
.UseConstructor = false;
serializer.Add(typeof (Derived), true)
.Add(1, "StringField")
.UseConstructor = false;
serializer.CompileInPlace();
using (var stream = new MemoryStream())
{
var message = new Derived(1, "Some text that is not important");
serializer.Serialize(stream, message);
stream.Position = 0;
var retrieved = (Derived) serializer.Deserialize(stream, null, typeof (Derived));
Assert.AreEqual(message.IntField, retrieved.IntField);
Assert.AreEqual(message.StringField, retrieved.StringField);
}
}
}
This exception goes away if I convert Base and Derived to mutable types. Am I doing anything wrong or is it a limitation of protobuf-net?