views:

58

answers:

2

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.

A: 

My guess would be that you need to add:

[ProtoInclude(1, typeof(MyClass1))]
[ProtoInclude(2, typeof(MyClass2))]

to both your MyClass1 and MyClass2since you inherit from MyInterface and the serialization wont know the type.

SwDevMan81
A: 

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.

Marc Gravell
It'll be great if you can share the patch....Also i was wondering if I can plug protobuf-net with AppFabric, have you tried that yet? btw..great work on the project !
ace
@ace email me (see profile) for the patch. Re ppfabroc, not since an early beta. But you can always just give it the blob :)
Marc Gravell
(sent via email)
Marc Gravell
Thanks Marc....
ace