Support for protobuf-net and interface

This question is directly related to the protobuf-net supporter, but someone else will comment.

I tried to serialize a class that contains a property that has an interface type, i.e.

[DataContract] public class SampleDataClass { [DataMember(Order=1)] public int Field1 { get; set; } [DataMember(Order = 2)] public IPayload Payload { get; set; } } [ProtoContract] [ProtoInclude(1, typeof(Payload))] public interface IPayload { int Field4 { get; set; } } [DataContract] public class Payload : IPayload { [DataMember(Order = 1)] public int Field4 { get; set; } } 

I managed to get this to work by changing the source of v1 protobuf-net. I did not see any problems with this approach while ProtoInclude is defined for the interface.

It is clear that for compilation I had to allow ProtoContract and ProtoInclude for design on interfaces, as well as several other changes here and there. (note, I would use DataContract / KnownType, but these attributes also cannot be decorated on interfaces)

Could you comment on possible flaws?

+1
source share
1 answer

The main failure that I see is that, in terms of payload, this moves the data into a sub-message. I have some similar projects around v2, which I hope will get by with this, preserving most of the values ​​in the main post. For reasons of sanity, I basically only had v2 in view of this change (since the two implementations would be separate, and v2 had a much better type model).

However, it should be possible to support both uses. If you want to send it as a patch for v1 (with the same license, etc.), I would love to see :)


This is available as a standard v2 feature.

+2
source

All Articles