What’s the difference between using the Serializable attribute and implementing the ISerializable interface?
What’s the difference between using the Serializable attribute and implementing the ISerializable interface?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you use the
SerializableAttributeattribute 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.The above indicates that the serializing facility should serialize the entire class
MyFoo, whereas:Using the attribute you can selectively choose which fields needs to be serialized.
When you implement the
ISerializableinterface, the serialization effectively gets overridden with a custom version, by overridingGetObjectDataand(and by providing a constructor of the formSetObjectDataMyFoo(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.