protobuf-net 和接口支持

发布于 2024-09-15 15:14:42 字数 770 浏览 3 评论 0原文

这个问题很大程度上直接向 protobuf-net 维护者提出,但其他人请发表评论。

我试图序列化一个包含具有接口类型的属性的类,即:

[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; }
}

我已设法通过更改 protobuf-net v1 的源来使其工作。 只要为接口定义了 ProtoInclude,我就没有发现这种方法有任何问题。

显然,为了编译它,我必须允许在接口上修饰 ProtoContract 和 ProtoInclude,以及各处的一些其他更改。 (注意,我会使用 DataContract/KnownType,但是这些属性也无法在接口上进行装饰)

您能否评论一下可能的缺点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撕心裂肺的伤痛 2024-09-22 15:14:43

我看到的主要问题是,就有效负载而言,这会将数据移动到子消息中。我在 v2 周围有一些类似的设计,希望能够解决这个问题,将大多数值保留在主要消息中。出于理智原因,我主要考虑的是 v2 进行此更改(因为这两个实现将是分开的,并且 v2 具有更好的类型模型)。

然而,应该可以支持两种使用模式。如果您想将其作为 v1 的补丁发送(具有相同的许可证等),我很乐意看一下:)


这是 v2 的标准功能

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文