Chapter 3 Serialization Techniques

Team Fly 

Page 59

Chapter 3
Serialization Techniques

IN CHAPTER 2 YOU learned how to read and write basic data types from and to files using streams. As far as file manipulation goes, Visual Basic got a real facelift. However, most practical applications don't store simple numeric values and strings to files. They need to store objects and, quite often, collections of objects. These objects may be built-in objects (such as Rectangle, Color, and other simple objects) or custom objects. This is where serialization comes in. Serialization is one of the truly exciting features introduced with the .NET Framework.

Serialization is the process of converting an arbitrary object, or collection of objects, into a stream of bytes, suitable for transmission to another process or another computer, or for persisting to a disk file. In the preceding chapter you learned how to store information to files using streams. When you use streams to write information to a file, or read it back from a file, you're responsible for formatting your data and writing them to the file. To read back the data, you must know what data types you're reading from the file and place them into appropriate variables.

Serialization goes beyond saving data to a file. It's a mechanism for saving an object in a way that makes it easy to reconstruct it later using the reverse process, which is called deserialization. Serializing an object means saving its properties. The serialization process doesn't persist the definition of an object, just its state. In other words, you can't serialize the methods of an object. You can serialize the values of its properties, so that you can later reconstruct an instance of the same object that will be in the same state as the object you serialized. The application that will deserialize the object must have access to the object's code (i.e., the class from which the object was instantiated), so that it can recreate the persisted object. Most importantly, you don't have to specify how each property is serialized. The serialization classes of the .NET Framework will determine how each data type is serialized and will read back the values of the serialized properties.

Serialization is not entirely new to the .NET Framework. VB6 programmers are familiar with the PropertyBag object, which we used to store instances of objects. .NET's serialization classes are more flexible and powerful and they go beyond the binary format.

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net