问题
this dataserializer is great for performance. but I keep getting stuck on datacolumns that has datatype of System.Object causing the serializer to throw an exception:Cannot serialize data column of type 'System.Object'.
is there any way around it?
回答1:
The protobuf format is designed to suit scenarios where the data is predictable to the receiver, and does not suit "object" scenarios very well, however, depending on the data layout a few things are possible:
- if the "object" data is a nested message of some kind, then you can mark the member as "DynamicType=true" as part of the ProtoMemberAttribute decoration; this then includes some metadata, but as a protobuf-net specific extension. It will not be very portable between systems
- if the "object" data is really one of a handful of known types, typically things like int/DateTime/float etc then there are some ways of representing that with inheritance (non-generic base type, multiple specific T subtypes). Again, this will not be very portable between platforms
I can illustrate either if I understand the model more.
回答2:
From the protobuf-net getting started page
Unlike XmlSerializer, the member-names are not encoded in the data - instead, you must pick an integer to identify each member.
In other words, you need to help the serializer by defining on each and every class how to serialize and deserialize it. If it is really a requirement to be able to automatically serialize all classes based on System.Object, protobuf is not for you.
来源:https://stackoverflow.com/questions/8953662/protobuf-data-serializer