| BINARY SERIALIZATION IN C# |
| Written by Christopher | ||||
| Sunday, 15 June 2008 18:37 | ||||
INTRODUCTIONIn 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 OBJECTIn 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 OBJECTDeserialization 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.
Set as favorite
Bookmark
Email This
Hits: 1886 Trackback(0)
Comments (1)
![]()
Stefan
said:
|
|
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. |
|
|
report abuse
vote down
vote up
|
Adobe InDesign CS3 - Working with objectsObjects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in... InDesign Videos | Christopher Read More |
Adobe InDesign CS3 - Working with Panels & MoreIn this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized... InDesign Videos | Christopher Read More |
Adobe Photoshop CS3 - Create customized rust on carsThis Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also... Photoshop Videos | Christopher Read More |
100% - + 3Show options |