What's the difference between using the Serializable attribute & implementing ISerializable?
Solution 1
When you use the SerializableAttribute
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, the serialization effectively gets overridden with a custom version, by overriding GetObjectData
and (and by providing a constructor of the form SetObjectData
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
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
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
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.
Related videos on Youtube
SoftwareGeek
Software Engineer & .NET Developer/Architect. I am passionate about application development with an eye towards quality & usability.
Updated on July 05, 2022Comments
-
SoftwareGeek almost 2 years
What's the difference between using the
Serializable
attribute and implementing theISerializable
interface?-
Jeff B about 7 yearsRelated: stackoverflow.com/q/5877808/945456
-
-
jrista about 14 yearsIf you implement ISerializable, it is also customary (or possibly even required) to implement the deserialization constructor: protected SomeClass(SerializationInfo info, StreamingContext context)
-
jrista about 14 yearsDeserialization is handled via the deserialization constructor. See my comment on segfaults answer.
-
Adam Lear about 14 yearsNote that you still have to mark the class [Serializable] even if you implement ISerializable interface.
-
hangy about 11 yearsIn which version of .NET is it okay to add the
Serializable
attribute to a property? MSDN says it can only be applied to classes, structs, enums and delegates. -
Daniel Leiszen over 10 yearsNone. Serializable attribute cannot be applied to properties, only class, struct and enum and delegate declarations.
-
nurchi about 10 yearsIt's the opposite: when the class is decorated with
SerializableAttribute
, a member can be marked withNonSerializedAttribute
to be skipped, as multiple people (and MSDN) said, when reconstructing a certain object is meaningless in a different environment, it is wise to not serialise it...