Is it possible to serialize interface-typed structs in protobuf-net, or does this only work for interface-typed classes?
I have a number of structs which I'd like to serialize, all of which implement an interface. I'm currently wrapping these in another struct (together with some meta data) in order to perform the serialization:
[ProtoContract]
public interface IData {}
[ProtoContract]
public struct MyData1 : IData { }
[ProtoContract]
public struct MyData2 : IData { }
[ProtoContract]
public struct SerializationStruct {
[ProtoMember(1)]
public int SomeMetadata;
[ProtoMember(2)]
public IData Content
}
From reading other answers (e.g. protobuf-net inheritance), I should be able to add the types to the RuntimeTypeModel
using AddSubType
and then serialize an instance of SerializationStruct
.
This works if I use classes for MyData1
and MyData2
but if I use structs (as above) I get the error:
System.ArgumentException: MyData1 is not a valid sub-type of IData. Parameter name: derivedType
when I attempt to add it to the model, like so:
RuntimeTypeModel.Default[typeof(IData)].AddSubType(1, typeof(MyData1));