Text Size

BINARY SERIALIZATION IN C#

PDF 

Tutorials

(3 votes, average: 4.33 out of 5)

INTRODUCTION

In a previous tutorial we talked about serialization and the various option for formatting its data output. In this document we will talk about binary serialization. Binary serialization is used for communication between .Net applications and is not a human-readable format. Windows relies on serialization for many important tasks such as remoting and copying items to the clipboard.

SERIALIZING AN OBJECT

In order to serialize an object you have to perform the following tasks:

 

·         Create a stream object to hold the serialized output.

·         Create a BinaryFormatter object since we want to perform a binary serialization. This object is located in the System.Runtime.Serialization.Formatters.Binary namespace.

·         Next, we need to call a method that serializes the objects and produces the appropriate output. This method is the Serialize method of the BinaryFormater object.

To successfully implement the serialization technique you have to add the following namespaces to be included in your program:

using System.IO;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

 

The following example demonstrates the proper use of the objects and methods of binary serialization.

 

  static void Main(string[] args)

        {

            string sData = " Data to be serialized";

            // Create file to save the data

            FileStream fs = new FileStream("Data", FileMode.Create);

            // Create binary formatter object:

            BinaryFormatter myBF = new BinaryFormatter();

            myBF.Serialize(fs, sData);

            fs.Close();

        }

In this example we have serialized a simple string object. You could try serializing an instance of a custom class or the current date for example. All objects can be serialized.

 

DESERIALIZING AN OBJECT

Deserialization of an object allows you to create a new object based on stored data. It actually restores an object with all its saved parameters and fields. The process is similar to the serialization of an object:

·         Create a stream object to hold the serialized output.

·         Create a BinaryFormatter object.

·         Create a new object to store the deserialized information.

·         Call the appropriate method to deserialize the data and cast them in the object.

The following example retrieves the serialized data of the previous example and restores a string object with the same text.

  static void Main(string[] args)

        {

           // Open file to read data from

            FileStream myFS = new FileStream("Data", FileMode.Open);

        // Create a binary formatter object to deserialize the data

            BinaryFormatter myBF = new BinaryFormatter();

            string RestoredData = "";

            RestoredData = (string)myBF.Deserialize(myFS);

            myFS.Close();

            Console.WriteLine(RestoredData);

            Console.ReadKey();

 

        }

 

The file stream wraps around the stored data. The binary formatter object reads from the file stream and deserializes the data. Then, the retrieved data are casted into a string object. The text of the string is printed on the console and we can see that it is exactly the string we entered in the first example. The last command in the program is similar to the Console.ReadLine method. It waits until we press a key, and then shows us which key was pressed.

Trackback(0)
Comments (1)add comment

Stefan said:

0
...
You write "Windows relies on serialization for many important tasks such as remoting and copying items to the clipboard". But how can one serialize Clipboard-Content as Clipboard.GetDataObject() will return a DatObject which is marked as nonserializable in its class definition.

I'm searching the whole web for a solution of this problem, but didn't find an answer.
 
August 01, 2008
Votes: +1

Write comment

busy

Site Statistics

Stats
Total Members
: 542
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1