Reputation: 2181
I'm unable to serialize my class using protobuf-net, the issue seems to be that protobuf-net is unable to serialize the interface.
interface MyInterface
{
string name;
}
[ProtoContract]
[ProtoInclude(1, typeof(MyClass1))]
[ProtoInclude(2, typeof(MyClass2))]
public abstract class ParentClass
{
[ProtoMember(1)]
List<MyInterface> elements;
}
[ProtoContract]
public class MyClass1 : ParentClass, MyInterface
{
[ProtoMember(1)]
int x;
}
[ProtoContract]
public class MyClass2 : MyInterface
{
[ProtoMember(1)]
string y;
}
I'm unable to serialize any object of type MyClass1, since the elements is a list of interface, which can be Mylass1 or MyClass2. I'm getting some encoding not set error.
Can anyone let me know how I can resolve this. Thanks.
Upvotes: 2
Views: 2599
Reputation: 1063884
In the current official versions I don't include interface serialization support. However, I do have a patch contributed (from another user) that seems to enable this.
I haven't applied this patch to the core yet, simply because I need to focus on finishing "v2" first before adding more features (especially as the feature would need to be completely re-implemented for v2), but I'm happy to share the patch with you if you would like.
Alternatively: use a base-class instead of an interface. That is supported (via [ProtoInclude]
) - however, the fact that your MyClass1
already has a parent class complicates matters somewhat.
Edit: this is now supported in v2. The code must know about the expected concrete implementations, obviously - but includes can now be attached to interfaces (or optionally specified in code for vanilla POCO models).
Upvotes: 1
Reputation: 50018
My guess would be that you need to add:
[ProtoInclude(1, typeof(MyClass1))]
[ProtoInclude(2, typeof(MyClass2))]
to both your MyClass1
and MyClass2
since you inherit from MyInterface
and the serialization wont know the type.
Upvotes: 0