protobuf-net 和接口支持
这个问题很大程度上直接向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到的主要问题是,就有效负载而言,这会将数据移动到子消息中。我在 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