Lesson 1: Serializing Objects in VB.NET

Generator QR in VB.NET Lesson 1: Serializing Objects

Lesson 1: Serializing Objects
QR Code JIS X 0510 Generator In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create QR Code 2d barcode image in VS .NET applications.
www.OnBarcode.com
Denso QR Bar Code Scanner In VB.NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
How to Deserialize an Object
Make Barcode In VB.NET
Using Barcode creator for Visual Studio .NET Control to generate, create bar code image in VS .NET applications.
www.OnBarcode.com
Decode Bar Code In Visual Basic .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Deserializing an object allows you to create a new object based on stored data. Essentially, deserializing restores a saved object. At a high level, the steps for deserializing an object are as follows: 1. Create a stream object to read the serialized output. 2. Create a BinaryFormatter object. 3. Create a new object to store the deserialized data. 4. Call the BinaryFormatter.Deserialize method to deserialize the object, and cast it to the correct type. At the code level, the steps for deserializing an object are easy to implement. The following Console application which requires the System.IO, System.Runtime.Serialization, and System.Runtime.Serialization.Formatters.Binary namespaces demonstrates how to read and display the serialized string data saved in an earlier example:
QR Maker In C#.NET
Using Barcode generator for VS .NET Control to generate, create QR image in VS .NET applications.
www.OnBarcode.com
Printing QR Code JIS X 0510 In Visual Studio .NET
Using Barcode printer for ASP.NET Control to generate, create QR-Code image in ASP.NET applications.
www.OnBarcode.com
' VB ' Open file from which to read the data Dim fs As FileStream = New FileStream("SerializedString.Data", FileMode.Open) ' Create a BinaryFormatter object to perform the deserialization Dim bf As BinaryFormatter = New BinaryFormatter ' Create the object to store the deserialized data Dim data As String = "" ' Use the BinaryFormatter object to deserialize the data from the file data = DirectCast(bf.Deserialize(fs), String) ' Close the file fs.Close ' Display the deserialized string Console.WriteLine(data) // C# // Open file from which to read the data FileStream fs = new FileStream("SerializedString.Data", FileMode.Open); // Create a BinaryFormatter object to perform the deserialization BinaryFormatter bf = new BinaryFormatter(); // Create the object to store the deserialized data string data = ""; // Use the BinaryFormatter object to deserialize the data from the file data = (string)bf.Deserialize(fs);
Encoding Quick Response Code In VS .NET
Using Barcode drawer for .NET framework Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications.
www.OnBarcode.com
Drawing UCC - 12 In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create UPC A image in VS .NET applications.
www.OnBarcode.com
5
Matrix Barcode Drawer In VB.NET
Using Barcode generator for .NET Control to generate, create Matrix Barcode image in .NET applications.
www.OnBarcode.com
Creating Code128 In VB.NET
Using Barcode encoder for VS .NET Control to generate, create Code 128 Code Set B image in .NET framework applications.
www.OnBarcode.com
Serialization
Create EAN-13 In VB.NET
Using Barcode creation for .NET framework Control to generate, create EAN13 image in .NET framework applications.
www.OnBarcode.com
Draw RM4SCC In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create RM4SCC image in Visual Studio .NET applications.
www.OnBarcode.com
// Close the file fs.Close(); // Display the deserialized string Console.WriteLine(data);
Code 128 Code Set A Generation In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create Code 128 Code Set C image in ASP.NET applications.
www.OnBarcode.com
Making Bar Code In Java
Using Barcode encoder for BIRT Control to generate, create bar code image in Eclipse BIRT applications.
www.OnBarcode.com
Deserializing a more complex object, such as DateTime, works exactly the same. The following code sample displays the day of the week and the time stored by a previous code sample:
Make PDF-417 2d Barcode In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
www.OnBarcode.com
Scan Code 128C In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
' VB ' Open file from which to read the data Dim fs As FileStream = New FileStream("SerializedDate.Data", FileMode.Open) ' Create a BinaryFormatter object to perform the deserialization Dim bf As BinaryFormatter = New BinaryFormatter ' Create the object to store the deserialized data Dim previousTime As DateTime = New DateTime ' Use the BinaryFormatter object to deserialize the data from the file previousTime = DirectCast(bf.Deserialize(fs), DateTime) ' Close the file fs.Close ' Display the deserialized time Console.WriteLine(("Day: " + (previousTime.DayOfWeek + (", Time: " _ + previousTime.TimeOfDay.ToString)))) // C# // Open file from which to read the data FileStream fs = new FileStream("SerializedDate.Data", FileMode.Open); // Create a BinaryFormatter object to perform the deserialization BinaryFormatter bf = new BinaryFormatter(); // Create the object to store the deserialized data DateTime previousTime = new DateTime(); // Use the BinaryFormatter object to deserialize the data from the file previousTime = (DateTime) bf.Deserialize(fs); // Close the file fs.Close(); // Display the deserialized time Console.WriteLine("Day: " + previousTime.DayOfWeek + ", Time: " + previousTime.TimeOfDay.ToString());
Code 39 Full ASCII Drawer In Objective-C
Using Barcode maker for iPad Control to generate, create Code 39 Full ASCII image in iPad applications.
www.OnBarcode.com
Code 128 Code Set C Generator In None
Using Barcode creation for Online Control to generate, create Code 128 Code Set C image in Online applications.
www.OnBarcode.com
Lesson 1: Serializing Objects
Generate PDF-417 2d Barcode In None
Using Barcode encoder for Office Word Control to generate, create PDF-417 2d barcode image in Office Word applications.
www.OnBarcode.com
Bar Code Drawer In Visual Studio .NET
Using Barcode creation for Reporting Service Control to generate, create bar code image in Reporting Service applications.
www.OnBarcode.com
As these code samples demonstrate, storing and retrieving objects requires only a few lines of code, no matter how complex the object is (assuming the object supports serialization, as discussed later in this chapter).
NOTE
The Inner Workings of Deserialization
Within the runtime, deserialization can be a complex process. The runtime proceeds through the deserialization process sequentially, starting at the beginning and working its way through to the end. The process gets complicated if an object in the serialized stream refers to another object. If an object references another object, the Formatter (discussed in more detail in Lesson 3 of this chapter, Custom Serialization ) queries the ObjectManager to determine whether the referenced object has already been deserialized (a backward reference), or whether it has not yet been deserialized (a forward reference). If it is a backward reference, the Formatter immediately completes the reference. However, if it is a forward reference, the Formatter registers a fixup with the ObjectManager. A fixup is the process of finalizing an object reference after the referenced object has been deserialized. Once the referenced object is deserialized, ObjectManager completes the reference.
Copyright © OnBarcode.com . All rights reserved.