none
Serializable RRS feed

  • Question

  • Hi ....


    I have a lot of question regard to serializable. Why the object is serializable? What is serializable? i dont exactly get it meaning from msdn ....

    What can i do when Object is serializable ?

    Plz give me some idea and explaination regard to serializable.

    Regard

    Hater
    Wednesday, January 2, 2008 6:54 AM

Answers

  •   Hater Clay wrote:

    Why the object is serializable? What is serializable


    If you need to persist state of an object, you can use serialization. It can be used when you send an object through network. Serialization converts object into XML format , and re-creates the object when it is deserialized. For making a class serializable, you can specify [serializable] attribute on top of the class.

      Hater Clay wrote:

    What can i do when Object is serializable ?


    When an object is serializable, it can be transported through a network. Or values of that object can be saved for future usage.
    Wednesday, January 2, 2008 7:26 AM
  •  

    Hi please find the following example how to use serialization . in the following example UserData class is marked as seriazable using [Serializable] attribute

     

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    SerializeObject();

    DeSerializeObject();

    }

    public void SerializeObject()

    {

    //This method writes the object to the file

    UserData data = new UserData();

    data.EMail = "user@user.com";

    data.Name = "User";

    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

    System.IO.Stream s = System.IO.File.Open("C:\\data.serialized",System.IO.FileMode.Create);

    bf.Serialize(s, data);

    s.Close();

    s.Dispose();

    }

    public void DeSerializeObject()

    {

    //This method reads the serialized object from the file and deserialize it

    UserData data = null;

    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

    System.IO.MemoryStream ms = new System.IO.MemoryStream();

    //System.IO.File f = new System.IO.File("C:\\data.serialized");

    System.IO.Stream s = System.IO.File.Open("C:\\data.serialized",System.IO.FileMode.Open);

    data = (UserData)bf.Deserialize(s);

    s.Close();

    s.Dispose();

    }

    }

     

    [Serializable]

    public class UserData

    {

    private string _Name;

    private string _EMail;

     

    public string Name

    {

    get { return _Name; }

    set { _Name = value; }

    }

    public string EMail

    {

    get { return _EMail; }

    set { _EMail = value; }

    }

    }

    Wednesday, January 2, 2008 8:45 AM
  • There are a hundred ways to do this, but most depend on your environment.

     

    By smart client, I'm going to assume are you talking a windows forms app.  What is the "server"?  There are a lot of different ways from using WCF, to web services, to http posting, to sockets (in that order of preference).

     

    If you are just starting out, and the server is not written, then I'd suggest reading up on Windows Communication Foundation (WCF).

     

    Good Luck,
    Chris.

     

    Thursday, January 3, 2008 5:06 PM

All replies

  •   Hater Clay wrote:

    Why the object is serializable? What is serializable


    If you need to persist state of an object, you can use serialization. It can be used when you send an object through network. Serialization converts object into XML format , and re-creates the object when it is deserialized. For making a class serializable, you can specify [serializable] attribute on top of the class.

      Hater Clay wrote:

    What can i do when Object is serializable ?


    When an object is serializable, it can be transported through a network. Or values of that object can be saved for future usage.
    Wednesday, January 2, 2008 7:26 AM
  •  

    Hi please find the following example how to use serialization . in the following example UserData class is marked as seriazable using [Serializable] attribute

     

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    SerializeObject();

    DeSerializeObject();

    }

    public void SerializeObject()

    {

    //This method writes the object to the file

    UserData data = new UserData();

    data.EMail = "user@user.com";

    data.Name = "User";

    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

    System.IO.Stream s = System.IO.File.Open("C:\\data.serialized",System.IO.FileMode.Create);

    bf.Serialize(s, data);

    s.Close();

    s.Dispose();

    }

    public void DeSerializeObject()

    {

    //This method reads the serialized object from the file and deserialize it

    UserData data = null;

    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

    System.IO.MemoryStream ms = new System.IO.MemoryStream();

    //System.IO.File f = new System.IO.File("C:\\data.serialized");

    System.IO.Stream s = System.IO.File.Open("C:\\data.serialized",System.IO.FileMode.Open);

    data = (UserData)bf.Deserialize(s);

    s.Close();

    s.Dispose();

    }

    }

     

    [Serializable]

    public class UserData

    {

    private string _Name;

    private string _EMail;

     

    public string Name

    {

    get { return _Name; }

    set { _Name = value; }

    }

    public string EMail

    {

    get { return _EMail; }

    set { _EMail = value; }

    }

    }

    Wednesday, January 2, 2008 8:45 AM
  • Okie ... Thanx for ur explanation
    But How can i implement the class or object is serializable ? Any idea regarding to this.

    Usage also if u dont mind me ..


    Thank for ur help

    Regard
    HaterClay
    Wednesday, January 2, 2008 8:56 AM
  • In order for a class to be serializable it must have the [Serializable] attribute applied to it.  See MS documentation about it.  Optionally, you can also implement the ISerializable interface to provide greater control on how the object itself is serialized / deserialized.

     

    A second route is to use XML Serialization, but you should really understand basic serialization before getting into that hot water!

     

     

    Good Luck,

    Chris.

     

    Wednesday, January 2, 2008 5:41 PM
  • Hi ...

    I really appriciated ur help and explaination abut the serializable. As ur explaination, I can pass the serialized object through the network. Actually, I am trying to write a program like smart client. The client pass the object to server through network. How can i achieve it ?


    Regard
    Hater Clay
    Thursday, January 3, 2008 2:55 AM
  • There are a hundred ways to do this, but most depend on your environment.

     

    By smart client, I'm going to assume are you talking a windows forms app.  What is the "server"?  There are a lot of different ways from using WCF, to web services, to http posting, to sockets (in that order of preference).

     

    If you are just starting out, and the server is not written, then I'd suggest reading up on Windows Communication Foundation (WCF).

     

    Good Luck,
    Chris.

     

    Thursday, January 3, 2008 5:06 PM