Important Interface in .NET: Work With ISerializable Interface

Welcome to the “Important Interface in C#” article series. In this series we will be talking about various useful interfaces of the .NET class library. In our previous article we have talked about the IEnumerable, ICollection, IComparable and ISerializable interfaces, you can read them here.

In today’s article we will learn the ISerializable interface in the .NET class library. We know that serialization is a feature by which we can preserve persistency of any object in the Microsoft platform. The ISerializable interface allows an object to control it’s own serialization and de-serialization.

Location

The ISerializable interface is located at:

Namespace: System.Runtime.Serialization

Assembly: mscorlib.dll

Syntax

The Syntax of the ISerializable interface is as in the following:

public interface ISerializable

It does not implement any interface.

Methods of ISerializable interface

It contains only one method, “GetObjectdata”, and it is:

GetObjectData: Populates a SerializationInfo with the data necessary to serialize the target object.

Now the point to discuss is, when is serialization needed? We know that serialization is a process that can persist the state of an object. The serialization is used when we want to send an object through some channel without losing its state.

For example, we have created one object but the object is not fully fledged. Some of the properties we have initialized and the remaining properties are dependent on another object / application that might produce results later.

For in this scenario we need to preserve the object state to initialize all of its properties that will be supplied later.

In this the following example we will serialize our own class by implementing ISerializable in a class. The ISerializable interface contains a GetObjectdata() method that we need to implement our own class to serialize the object of this class.

In the following example we have created a “Person” class and an object of the Person class we will serialize using XML serialization.

Have a look at the implementation of the “Person” class. We have created both an empty constructor/default constructor and a parameterized constructor. But we are not using an empty constructor. We need to keep in mind that when we implement XML serialization the empty constructor must be present in the class. In the following example we have implemented XML serialization with an object of the “Person” class.

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.Serialization;

using System.Text;

using System.Xml.Serialization;

 

namespace SelfHostingWebAPI

    public class Person : ISerializable

    {

        public string name { get; set; }

        public string surname { get; set; }

        public Person(){}

        public Person(string name, string surname)

        {

            this.name = name;

            this.surname = surname;

        }

 

        public void GetObjectData(SerializationInfo info, StreamingContext context)

        {

            info.AddValue("name", this.name);

            info.AddValue("surname", this.surname);

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            Person p = new Person("Sourav","Kayal");

            XmlSerializer oSerialiser = new XmlSerializer(typeof(Person));

            Stream oStream = new FileStream(@"D:\mydata.xml", FileMode.Create);

            oSerialiser.Serialize(oStream, p);

            oStream.Close();

 

            Console.ReadLine();

        }

    }

}

We have specified the file path in the “D\” location in this example; obviously you can choose your preferred location. Here is the output of the example above.

ISerializable Interface

Conclusion

In this article we have learned the concept of an ISerializable interface and the serialization process using an ISerializable interface. In the example we saw XML serialization in action. Though, we are free to use binary serialization using a very similar process. In a future article we will understand a few more important interfaces from the .NET class library.
 


Similar Articles