Creating Serializable Objects

Team Fly 

Page 65

The last few statements in the event handler extract the two objects (the Rectangle and Bitmap object) from the ArrayList and display some of their basic properties. The last statement retrieves the bitmap stored in the second item of the reconstructed ArrayList, casts it to the Bitmap type, and then uses it as the form's background image. The code displays the bitmap on the form to demonstrate that the bitmap was preserved during the serialization process.

BINARY SERIALIZATION PRESERVES TYPE FIDELITY

If you serialize the same ArrayList in SOAP and binary formats, you'll realize that the sizes of the two files are very different. The file that stores the binary representation is actually much smaller than the file with the SOAP representation of the same object. The binary file's size is nearly the same as the image file, with a few dozen more bytes for the Rectangle object. The SOAP file is nearly twice as large. The difference is not due to any form of compression; the SOAP file is quite verbose.

This remark brings us to a very important point about the various types of serialization: type fidelity. Binary serialization preserves type fidelity, because it has stored the bitmap as a GIF file. The SOAP serialization, on the other hand, has stored the actual bitmap, without the compression built into the GIF file. If you open the Objects.bin file generated by the SoapFormatter, you'll see that the image's bytes are encoded in Base64, but the element under which the bitmap is stored is called ''Bitmap." In other words, the SoapFormatter serialized the actual bitmap, not the GIF file from which the Bitmap object was constructed.

In most cases, such extreme fidelity won't matter. After all, a bitmap is a bitmap, and as long as you can reconstruct the image from the serialized data you shouldn't care how the image was serialized.

An object can be serialized in binary and SOAP format with the same statements. You can uncomment the statements that refer to the SoapFormatter and comment out the statements that refer to the BinaryFormatter to test both serialization techniques. Keep in mind that binary serialization uses the CLR data types and generates the most accurate representation of the objects. However, it's limited to .NET; you can't use a binary serialized object outside .NET. If you need to exchange serialized objects with other systems, use SOAP, or XML, serialization.

Creating Serializable Objects

Just about any custom class created in .NET can be serialized, as long as it's marked with the <Serializable> attribute. Many of the built-in objects are serializable, but not all of them. Unfortunately, nonserializable .NET classes are not clearly marked as such in the documentation. As you can understand, all basic data types in .NET are serializable, and so are some of the collections (arrays and ArrayLists are serializable, but the HashTable isn't). If you want to serialize an ArrayList with numbers, or strings, you don't have to create a custom class, or do anything special. You simply call the Serialize method of the appropriate Serializer class and the collection will be persisted to a stream, which in turn will move the serialized data to a disk file.

.NET programming means developing and using custom classes. If you want to be able to serialize your custom classes, all you have to do is prefix their declaration with the <Serializable> attribute. You don't have to mark any of the class's public fields as serializable, but you can exclude selected fields from the serialization process by marking them with the <NonSerializable> attribute. Fields

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