问题
I have a server running on MS.NET and a client on Mono (this is a Unity3D engine) and when i try to BinaryFormatter().Deserialize an object like this:
[Serializable]
public class Simulator
{
public IDictionary<int, Task> tasks = new Dictionary<int, Task>();
the client side cannot found/load types: Dictionary, List... The same "client code" running under MS.NET works good i.e. does not have any exceptions during deserialization.
As i read from http://www.mono-project.com/FAQ:_Technical#Compatibility this is a common problem:
"If you are serializing your own classes, there is no problem, since you have control over the assemblies and classes being used for serialization. However, if you are serializing objects from the framework, serialization compatibility is not guaranteed, since the internal structure of those objects may be different. This compatibility is not even guaranteed between different MS.NET versions or Mono versions."
Does ProtoBuf-Net help to avoid/resolve this serialization/deserialization problem ?
回答1:
Yes, an external serialization tool such as protobuf-net would solve this - indeed, once you have serialization working between platforms (C++ to java to python to .net), framework versions are less of a concern.
So yes: data serialized in protobuf-net on mono / unity is fully compatible when loaded on .NET. However, it should be noted that BinaryFormatter
and protobuf-net are not direct 1:1 equivalents - each has slightly different features and behaviors. For example, protobuf-net doesn't walk events/delegates, and doesn't usually play nicely with things known only as "object". However, key/common scenarios like dictionary and list are fully supported.
来源:https://stackoverflow.com/questions/12504275/can-i-serialize-an-object-containing-members-dictionary-list-etc-in-mono