Class binary serialization c#
In the previous post , we discussed XML serialization. In this short article we are going to take a look at binary serialization via the BinaryFormatter class. As we mentioned in a previous article , binary serialization in. NET converts an object or entire object graph into a binary format that is not human readable. It preserves type fidelity which is quite useful when utilizing objects across multiple applications of instances of the same application. Before we can use binary serialization, we have to add a reference to the System.
Right-click the project, select Add Reference, then select this assembly in the dialog. Now that we have created our Sample class, decorated it with the Serializable attribute, and added a reference to the System. The full code for our Program class is shown below. We will open the file with Wordpad so that the text wraps and so that we can see the contents.
The binary output does have some readable text. The first thing you should notice is that the entire assembly and namespace information is stored with the serialized object.
Be sure not to save the file because we are going to use this same file in the code below to deserialize our object. Now this was a very simple example but understand that any object that supports serialization can be serialized using the BinaryFormatter.
If we have an object graph that contained multiple levels of objects in a hierarchy, we could serialize these objects in exactly the same way that we just did.
The process can be automated to a large extent, provided you are given sufficient control over the process. For example, situations may arise where simple binary serialization is not sufficient, or there might be a specific reason to decide which fields in a class need to be serialized.
The following sections examine the robust serialization mechanism provided with. NET and highlight a number of important features that allow you to customize the process to meet your needs. The state of a UTF-8 or UTF-7 encoded object is not preserved if the object is serialized and deserialized using different. Binary serialization can be dangerous. Never deserialize data from an untrusted source and never round-trip serialized data to systems not under your control.
As the nature of binary serialization allows the modification of private members inside an object and therefore changing the state of it, other serialization frameworks like JSON. NET Core supports binary serialization with a subset of types. You can see the list of supported types in the Serializable types section. The defined set of types are guaranteed to be serializable between.
NET implementations, such as Mono, aren't officially supported but should also be working. Serialization Concepts Discusses two scenarios where serialization is useful: Selective Serialization Describes how to prevent some members of a class from being serialized.
Custom Serialization Describes how to customize serialization for a class by using the ISerializable interface. Steps in the Serialization Process Describes the course of action serialization takes when the Serialize method is called on a formatter.