简体   繁体   中英

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. I have a class that i would like to use in a message but its not in my source.

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. 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. 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.

The better option here is to look at "v2", which is available as an alpha download. Your attributed types should be fine, but you can also tell the type-model how to handle unattributed types; this is all via RuntimeTypeModel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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