问题
Is it possible to supply your own methods for serializing and deserializing a type for protobuf-net to use in its' Serializer.Serialize() and Serializer.Deserialize() methods?
i.e. Write my own code using ProtoWriter and ProtoReader, instead of marking up the class with serialization attributes.
回答1:
The mention of ProtoReader
/ ProtoWriter
means this is a "v2" question, in which case you don't have to use the attributes at all - you can define the model at runtime via TypeModel
. Re the question of custom serializers - at the moment, not exactly - but I expect I'll add an interface for this before long. The only problem is that the caller is then responsible You might also want to look at the experimental DataTable
serializer, which does something similar.
There are some technical considerations too, though; in particular inheritance; the obvious pattern would be for your class to implement an interface, but this means that once the object is created it is too late to create a subclass. Unless the type that implements the interface isn't the entity itself, but a separate type that juts handles serialization/deserialization...
来源:https://stackoverflow.com/questions/4523165/custom-serialization-deserialization-methods-in-protobuf-net