Should I Still Use BinaryFormatter for Simple Serialization in .NET 4.0?

筅森魡賤 提交于 2020-01-24 03:05:38

问题


I am developing a master-slave style application. The master application will send state data to the slave(s) to process and display at some constant rate. The state data is wrapped up into a single class that contains many fields. These field types consist of primitives, classes, interfaces, lists of interfaces, and so on. All the types are either BCL or custom types, so the the custom types can be modified if necessary. Both the master and and slave applications will be .NET 4.0. I am not concerned with serialization versioning as the master and slave applications will be delivered as a pair.

I need a "quick" way to serialize the state data on the master and deserialize it on the slaves. When I say "quick", I am more talking about development time (but processing time could be a factor if the solution was terrible). However, the master and slaves will be distributed over a WAN, so some level of compactness would be nice also.

For a quick solution, I am currently thinking about simply using BinaryFormatter and then compressing the stream with GZipStream. Is this the way to go for .NET 4.0?


回答1:


If speed-of-development is the key (especially since you have interfaces etc, which you need to configure appropriately for some serializers) then maybe. Just remember to mark any events as:

[field:NonSerialized]

On every other measure (CPU performance, bandwidth, robustness as you version, interoperability, cost of maintenance, etc) I would choose other formats :)

Here's a selection profiled:

Performance Tests of Serializations used by WCF Bindings

Perhaps not a surprise (since I wrote it), but I lean towards protobuf-net...




回答2:


Have a look at Protocol Buffers here: Fast and compact object serialization in .NET



来源:https://stackoverflow.com/questions/5284641/should-i-still-use-binaryformatter-for-simple-serialization-in-net-4-0

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