What's the difference between using the Serializable attribute & implementing ISerializable?

C#InheritanceAttributesSerializationIserializable

C# Problem Overview


What's the difference between using the Serializable attribute and implementing the ISerializable interface?

C# Solutions


Solution 1 - C#

When you use the [SerializableAttribute](http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx "MSDN reference page") attribute you are putting an attribute on a field at compile-time in such a way that when at run-time, the serializing facilities will know what to serialize based on the attributes by performing reflection on the class/module/assembly type.

[Serializable]
public class MyFoo { … }

The above indicates that the serializing facility should serialize the entire class MyFoo, whereas:

public class MyFoo
{
    private int bar;

    [Serializable]
    public int WhatBar
    {
       get { return this.bar; }
    }
}

Using the attribute you can selectively choose which fields needs to be serialized.

When you implement the [ISerializable interface](http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx "MSDN reference page"), the serialization effectively gets overridden with a custom version, by overriding [GetObjectData](http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.getobjectdata.aspx "MSDN reference page") and SetObjectData (and by providing a constructor of the form MyFoo(SerializationInfo info, StreamingContext context)), there would be a finer degree of control over the serializing of the data.

See also this example of a custom serialization here on StackOverflow. It shows how to keep the serialization backwards-compatible with different versionings of the serialized data.

Hope this helps.

Solution 2 - C#

The SerializableAttribute instructs the framework to do the default serialization process. If you need more control, you can implement the ISerializable interface. Then you would put the your own code to serialize the object in the GetObjectData method and update the SerializationInfo object that is passed in to it.

Solution 3 - C#

The ISerializable interface lets you implement custom serialization other than default. When you implement the ISerializable interface, you have to override GetObjectData method as follows

public void GetObjectData (SerializationInfo serInfo, 
                                    StreamingContext streamContext)
{
   // Implement custom Serialization
}

Solution 4 - C#

ISerialize force you to implement serialization logic manially, while marking by Serializable attribute (did you mean it?) will tell Binary serializer that this class can be serialized. It will do it automatically.

Solution 5 - C#

Inheriting from ISerializable allows you to custom implement the (de)serialization. When using only the Serializable attribute, the (de)serialization can be controlled only by attributes and is less flexible.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSoftwareGeekView Question on Stackoverflow
Solution 1 - C#t0mm13bView Answer on Stackoverflow
Solution 2 - C#SegfaultView Answer on Stackoverflow
Solution 3 - C#AsadView Answer on Stackoverflow
Solution 4 - C#AndreyView Answer on Stackoverflow
Solution 5 - C#logicnpView Answer on Stackoverflow