Can I serialize an object (containing members: Dictionary, List… etc) in Mono and deserialize it in MS.NET or vice versa by using protobuf-net?

有些话、适合烂在心里 提交于 2019-12-10 16:27:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!