简体   繁体   English

如何将 class 添加到 protobuf-net

[英]How to add a class to protobuf-net

i'm using protobuf-net and i like it so far but one thing is bugging me.我正在使用 protobuf-net,到目前为止我很喜欢它,但有一件事困扰着我。 I have a class that i would like to use in a message but its not in my source.我有一个 class,我想在消息中使用它,但它不在我的源中。

like so:像这样:


[ProtoContract]
class Test{ 
    [ProtoMember(1)]
    public Otherclass Attr {get;set}
}
but ProtoBuf does not know how to serialize Otherclass and i cant access the code of it. 但 ProtoBuf 不知道如何序列化 Otherclass,我无法访问它的代码。 So i need to tell it how to do it but how? 所以我需要告诉它怎么做,但是怎么做?

In v1 you can't, unless it happens to have xmlaerializer or datacontractserializer attributes.在 v1 中你不能,除非它碰巧有 xmlaerializer 或 datacontractserializer 属性。 Well, that isn't quite true... There is a GlobalOptions toggle that should work in theory, but is not very tolerant if the type changes, as it is inferring a lot.好吧,这不太对……理论上应该一个GlobalOptions切换,但如果类型发生变化,它就不是很宽容,因为它推断出很多。

The better option here is to look at "v2", which is available as an alpha download.这里更好的选择是查看“v2”,它可以作为 alpha 下载。 Your attributed types should be fine, but you can also tell the type-model how to handle unattributed types;您的属性类型应该没问题,但您也可以告诉类型模型如何处理未属性类型; this is all via RuntimeTypeModel.这都是通过 RuntimeTypeModel 实现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM