1

I'm having a hard time understanding the need for the ISerializable interface... I guess I'm missing something pretty important in this subject, so I'd appreciate it if somebody could give me a hand.

This works perfectly well -

[Serializable]
    class Student
    {
        public int age;
        public string name;

        public Student()
        {
            age = 0;
            name = null;
        }
    }
 class Program
    {
        public static void Main()
        {
            Stream stream = File.Open("Test123.txt", FileMode.Create);

            BinaryFormatter bf = new BinaryFormatter();

            Student s1 = new Student();
            s1.name = "Peter";
            s1.age = 50;
            bf.Serialize(stream, s1);

            stream.Close();

            Stream stream2 = File.Open("Test123.txt", FileMode.Open);

            Student s2 = (Student)bf.Deserialize(stream2);

            Console.WriteLine(s2.age);

        }

And it worked without implementing ISerializable and without overriding GetObjectData(). How so? What is the use of the interface then?

Thanks.

thomas
  • 1,133
  • 1
  • 12
  • 31
  • possible duplicate of [What is the point of the ISerializable interface?](http://stackoverflow.com/questions/810974/what-is-the-point-of-the-iserializable-interface) – nawfal Jul 10 '14 at 12:39

2 Answers2

5

Serializable uses default serialization. The point of the ISerializable interface is to override serialization so that you can have your own.

Marcus
  • 8,230
  • 11
  • 61
  • 88
  • 1
    And I should desire a 'custom' serialization process when I want to, say, only convert some of the data of the object? – thomas Oct 13 '13 at 12:47
  • Normally, you don´t need custom serialization. If you especially desire to use your own formatter, e.g. BinaryFormatter, SoapFormatter etc. then you might want to implement the ISerializable interface. – Marcus Oct 13 '13 at 12:49
  • @Marc Gravell elaborated very good on this topic: http://stackoverflow.com/questions/810974/what-is-the-point-of-the-iserializable-interface – Marcus Oct 13 '13 at 12:50
  • Sorry, I read your follow-up question wrong, yes, that would be one reason to use custom serialization. – Marcus Oct 13 '13 at 12:53
2

In addition to @Marcus answer, Serializable and ISerializable only apply for the *Formatter (typically, BinaryFormatter and SoapFormatter) serializers built into .Net

If the attribute is there but the interface is not, they will use reflection to find all the properties and their values.

Different serializers have different ways of customizing serialization (all though most have an option to just use reflection)

XmlSerializer for example has a different interface, IXmlSerializable and also doesn't check for the Serializable attribute. check out the docs for what ever serializer you're using to see what the story is. Wcf for example uses the DataContract attributes to determine how and what to serialize

aL3891
  • 6,205
  • 3
  • 33
  • 37