oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

C# Object Serialization

by Budi Kurniawan

The .NET Framework class library provides four namespaces to support object serialization: System.Runtime.Serialization, System.Runtime.Serialization.Formatters, System.Runtime.Serialization.Formatters.Binary, and System.Runtime.Serialization.Formatters.Soap.

Related Reading

Programming C#
By Jesse Liberty

To serialize an object, you need to either mark the object class with the [Serializable] attribute or implement the ISerializable interface. The following is a class that is marked with the [Serializable] attribute:

public class MySerializableClass

If a class to be serialized contains references to other objects, the classes of those other objects must also be marked [Serializable] or implement the ISerializable interface.

The ISerializable Interface

Unless you mark the class with the [Serializable] attribute, this interface must be implemented by all classes with serialized instances. Use the ISerializable interface if you want your class to control its own serialization and de-serialization.

The ISerializable interface only has one method, GetDataObject, to which you pass a SerializationInfo object and a StreamingContext object. The GetDataObject method will then populate the SerializationInfo object with the data necessary for the serialization of the target object.

The following code illustrates a class named MySerializableClass that implements the System.Runtime.Serialization.ISerializable interface. A SerializationInfo object and a StreamingContext object are available from the GetObjectData method.

  using System.Runtime.Serialization;

  public class MySerializableClass : ISerializable
    SerializationInfo info;
    StreamingContext context;

    public void GetObjectData(SerializationInfo info, 
	                          StreamingContext context) 
    { = info;
      this.context = context;

    // implementation code goes here

Classes that implement this interface include System.Data.DataSet, System.Drawing.Font, System.Collections.Hashtable, System.Drawing.Icon, and System.Drawing.Image.

The IFormatter Interface

IFormatter is a member of the System.Runtime.Serialization namespace. This interface is implemented by both the BinaryFormatter class and the SoapFormatter class. This interface has only two methods, Serialize and de-serialize. The Serialize method is used to serialize an object or an object containing other objects. The signature of this method is:

void Serialize( Stream serializationStream As Stream, 
     Object graph As Object );

The de-serialize method is used to de-serialize an object or an object containing other objects. This method has the following signature:

Object de-serialize( Stream serializationStream ) ;

Because the return value of the de-serialize method is an object of type Object, you need to downcast the object into the appropriate type. Two formats are available to store your persisted objects: binary and XML. Normally, you upcast either a BinaryFormatter object or a SoapFormatter object as an IFormatter object, depending on the format you use for the serialization.

The BinaryFormatter Class

The BinaryFormatter class is the only member of the System.Runtime.Serialization.Formatters.Binary namespace. You use this class to serialize and de-serialize an object or a graph of objects in a binary file. For performance, this is the format you need to choose when serializing your object(s). The resulting file is also more compact than the XML format.

The SoapFormatter Class

This is the only member of the System.Runtime.Serialization.Formatters.Soap namespace. You use this class to serialize and de-serialize an object or a graph of objects in XML format.

A Vector-Based Drawing Application

To illustrate the object serialization technique, the following is a simple vector-based drawing application. The user can draw three types of shapes: rectangles, lines, and ellipses, represented by the Rect, Line, and Ellipse classes, respectively. The three classes implement the IShape interface. The IShape and the three classes implementing it are given in Example 1. Note that all classes are marked with the [Serializable] attribute.

Pages: 1, 2, 3, 4

Next Pagearrow