I'm trying to serialize a model using the great Protobuf-NET. I cannot use the attributes (objects are unknown at compile-time),so I constructed a TypeModel. My object model consist of a class TestDataObject, this class has a property of ITestDataExtension. The abstract baseclass TestDataExtensionBase implements this interface and the class TestDataExtension (myDataObjectExtA in code) inherits from this baseclass.
My TypeModel is constructed like this:
System.IO.MemoryStream tmpMemoryStream = new System.IO.MemoryStream();
RuntimeTypeModel model = TypeModel.Create();
MetaType basetype = model.Add(typeof(TestDataObject), true);
MetaType interfaceType = model.Add(typeof(ITestDataExtension), true);
//MetaType extBaseType = interfaceType.AddSubType(100, typeof(TestDataExtensionBase));
MetaType extType = interfaceType.AddSubType(200, myDataObjectExtA.GetType());
model.Add(typeof(TestDataExtensionBase), true);
model.Add(myDataObjectA.Ext.GetType(), true);
model.CompileInPlace();
model.Serialize(tmpMemoryStream, myDataObjectA);
byte[] tmpDat = tmpMemoryStream.ToArray();
If I run the following the properties of the base class are not serialized, and I need them to be serialized.
In my opinion I should have added a subtype for the TestDataExtensionBase like this:
MetaType extBaseType = interfaceType.AddSubType(100, typeof(TestDataExtensionBase));
MetaType extType = extBaseType.AddSubType(200, myDataObjectExtA.GetType());
But this results in a : Unexpected sub-type: TestDataExtension. Does anyone known what I'm doing wrong? Any help would be appreciated.
2 issues:
I think the following does what you describe...?