This Blog Has Been Moved !

This Blog Has been moved to http://aleemkhan.wordpress.com

Serialization is used to persist the state of the object, so the object can be regenerated with the same state later. .NET provides different types of serializations for objects,

  • BinaryFormatter for bunary serialization
  • XMLSerializer class for XML Serialization
  • SoapFormatter class for Soap Serialization.

You can use these objects for serialization. The Framework also needs to serialize the objects at various places for example in case of web services, the objects needs to be serialized for communication, in case of Remoting and also if the object is to be passed across the boundary of an Application Domain or the process. In all these cases, the framework will only be able to successfully serialize your objects if your objects are serializable.  

All the basic .NET types are serializable except Object, there are various other base classes which are by default serializable and can be successfully used in above scenarios, this includes the very commnly used DataSet, DataTable and other class, the complete list is

These classes are serializable because they implement the ISerializable Interface.

To set your own types Serializable, the most common way is to set the class/type with the [System.Serializable()] attribute, also use the [System.NonSerialized()] for the members that you don’t want to serialize. See the following example

 

[System.Serializable]

class TestClass

{

      private string str = "Check";

      [System.NonSerialized()]

      private SqlCommand cmd = new SqlCommand();

      private int count;

     

      public CheckFunction()

      {

            ........

            ........

      }

}

 

However, using the serializable attribute is not the only way, and in some cases is not enough, specially if you are serializing complex objects which are inherited from other base objects. To serialize such objects you can have better control over serialization by implementing the ISerializable interface. This interface has only one method, which is GetObjectData(), this method is called when the object is to be serialized and you need to provide an extra constructor in the class to provide the de-serialization mechanism to regenerate the object. While serialization in GetObjectData() method, you can add your values to SerializationInfo parameter. During de-serialization you can get the values from the SerializationInfo object and set the values to the members of the class to regenarte the object in the same state. The example below will give you can idea

 

class TestClass : ISerializable

{

      private string str = "Check";

      private int count;

     

public TestClass(SerializationInfo info, StreamingContext context)

{

            this.str = info.GetString("str");

            this.count = info.GetInt32("count");

      }

 

      public void GetObjectData(SerializationInfo info, StreamingContext context)

      {

            // TODO:  Add TestClass.GetObjectData implementation

            info.AddValue("str",str);

            info.AddValue("count",count);

      }

}

 

Generally, as the MS guidelines and PAG documents say, serialization is not recommended for your custom objects, it has various issues regarding performance, security and scalability. But sometimes you have no other option then to use serialization. PAG guidelines do not recommend serialization and due to valid reasons but do not tell any other way you could get around the typical problems without serialization.

 

Comments

8 comments have been posted.
Posted @ 6:26 PM
Hi Aleem,

My comment is not related to .NET serialization. But I like you to see this like and enjoy. http://www.thebestpageintheuniverse.net/c.cgi?u=banish

Take Care
 

Anonymous Touseef Liaqat
Posted @ 6:28 PM
2nd like = link
 

Anonymous Touseef Liaqat
Posted @ 6:32 PM
Aaaj kal kya ho raha hai KHAN sahaab? Taraqiaan kahaan tak pohanchieen? No new post here from you recently? buzy?