BinaryFormatter进行序列化 [英] BinaryFormatter for serialization

查看:75
本文介绍了BinaryFormatter进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发自己的远程桌面C#程序.我想知道是否可以使用BinaryFormatter序列化自己的对象例如"Packet",其中包含BitMap屏幕快照的属性,鼠标坐标以及一些文本.然后客户会反序列化它.从理论上讲,这可能是一种舒适的方法.有可能吗?

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?

推荐答案

这是可能的,但我不鼓励这样做. BinaryFormatter 算法是专有的,因此使用此类数据编写非.NET应用程序将非常困难.该格式在过去已经更改,并且将来可能会更改,因此将其用于您希望将来使用新的.NET版本再次打开的持久性数据是不安全的.我认为 BinaryFormatter 最初是为在一台计算机上的进程之间传递数据而设计的,而不是为持久化数据而设计的.

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 对于包含多个属性的数据效率不高,因为它需要反序列化整个对象才能访问任何字段.如果数据包含图像之类的大数据,则尤其有问题.

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.

如果您的数据不需要随机访问,建议将其序列化为JSON或XML之类的文本格式.如果数据很大,则应考虑压缩文本编码的数据.

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.

如果您需要随机访问数据,则应调查MySQL或SQLite之类的数据存储.

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

这篇关于BinaryFormatter进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆