BinaryFormatter for serialization

白昼怎懂夜的黑 提交于 2019-12-20 05:27:07

问题


I am working on my own remote desktop C# program. I wondered if I could Serialize using BinaryFormatter an object of my own for example "Packet" that contains properties of BitMap screenshot, mouse cordinates and maybe some text. And then the client would deserialize it. Theoretically it might be a comfortable way to do this. Is it possible?


回答1:


BinaryFormatter is not a good choice for serialization over the network or to persistent storage, it is designed for serialization when you are communicating within the same machine. It is extremely intolerant of version differences of any assembly referenced in your serialized object on opposite ends of the connection, I personally have had a windows updates not being applied on one machine to cause the two sides to not be able to communicate.

Instead use a serializer more designed for network communication like DataContractSerializer or a 3rd party binary serializer like ProtoBuf-net.




回答2:


This is possible, but I discourage it. The BinaryFormatter algorithm is proprietary, so it will be very difficult to write non-.NET applications using such data. The format has changed in the past, and may change in the future, so it is unsafe to use it for persistent data you expect to open again in the future with a new .NET version. I think BinaryFormatter was originally designed for passing data between processes on one machine, not for persisting data.

BinaryFormatter is inefficient for data that contains multiple properties because it requires deserializing the entire object to access any field. This is especially problematic if the data contains large data like images.

If your data does not require random access, I suggest serializing it to a text format like JSON or XML. If the data is large, you should consider compressing the text-encoded data.

If you require random-access to data you should investigate data stores like MySQL or SQLite.



来源:https://stackoverflow.com/questions/30921426/binaryformatter-for-serialization

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