protobuf-net and interface support

若如初见. 提交于 2019-12-20 03:56:17

问题


This question directly in large part to the protobuf-net maintainer(s) but anyone else please comment.

I was trying to serialize a class that contains a property which has an interface type, ie:

[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 have managed to get this to work by changing the source of v1 of protobuf-net. I did not see any problem with this approach as long as ProtoInclude is defined for the interface.

Clearly to get this to compile I had to allow ProtoContract and ProtoInclude to be decorated on interfaces, plus a few other changes here and there. (note, I would have used DataContract/KnownType however these attributes are also not able to be decorated on interfaces)

Can you please comment on possible shortcomings?


回答1:


The main glitch I can see is that in terms of payload this moves the data into a sub-message. I have some similar designs around v2 that hopefully get around this, keeping most values in the primary message. For sanity reasons, I mainly had just v2 in mind for this change (since the two implementations would be separate, and v2 has a much better type model).

However, it should be possible to support both modes of use. If you want to send it as a patch for v1 (with the same license etc) I'd happily take a look :)


This is available as a standard feature of v2



来源:https://stackoverflow.com/questions/3523225/protobuf-net-and-interface-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!