Popular Posts

Dec 11, 2012

Object Serialization - C#

Writing data to the disk as text is always dangerous. Any user can open the text file and easily read the data. With Object Serialization.

Serialization is the process of converting complex objects into stream of bytes for storage. Deserialization is its reverse process, that is unpacking stream of bytes to their original form. The namespace which is used to read and write files is System.IO. For Serialization we are going to look at the System.Runtime.Serializationnamespace. The ISerializable interface allows to make any class Serializable.

- Create a console project.add a class named it Employee
[Serializable()] //Set this attribute to all the classes that you define to be serialized
    public class Employee : ISerializable
        public int EmpId;
        public string EmpName;

        //Default constructor
        public Employee()
            EmpId = 0;
            EmpName = null;

        //Deserialization constructor.
        public Employee(SerializationInfo info, StreamingContext ctxt)
            //Get the values from info and assign them to the appropriate properties
            EmpId = (int)info.GetValue("EmployeeId", typeof(int));
            EmpName = (String)info.GetValue("EmployeeName", typeof(string));

        //Serialization function.
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
            //You can use any custom name for your name-value pair. But make sure you
            // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
            // then you should read the same with "EmployeeId"
            info.AddValue("EmployeeId", EmpId);
            info.AddValue("EmployeeName", EmpName);

 static void Main(string[] args)
            //Create a new Employee object
            Employee mp = new Employee();
            mp.EmpId = 204;
            mp.EmpName = "Jubayer";

            // Open a file and serialize the object into it in binary format.
            // EmployeeInfo.osl is the file that we are creating. 
            // Note:- you can give any extension you want for your file
            // If you use custom extensions, then the user will now 
            //   that the file is associated with your program.
            Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();

            Console.WriteLine("Writing Employee Information");
            bformatter.Serialize(stream, mp);

            //Clear mp for further usage.
            mp = null;

            //Open the file written above and read values from it.
            stream = File.Open("EmployeeInfo.osl", FileMode.Open);
            bformatter = new BinaryFormatter();

            Console.WriteLine("Reading Employee Information");
            mp = (Employee)bformatter.Deserialize(stream);

            Console.WriteLine("Employee Id: {0}", mp.EmpId.ToString());
            Console.WriteLine("Employee Name: {0}", mp.EmpName);

Download Code 

No comments:

Post a Comment