Using ISerializable for custom serialization to a FileStream
If you want more control over what is serialized, you should implement ISerializable
on your object. This gives a developer complete control over what is serialized. Note that you still need to add the [ISerializable]
attribute to your object. Lastly, the developer also needs to implement a deserialization constructor. Using ISerializable
, however, does have a caveat. According to the MSDN, the forward compatibility of your object with newer versions of the .NET Framework and any improvements made to the serialization framework might not be applicable on your object. You also need to implement ISerializable
on all the derived types of your object.
Getting ready
We will create a new class that wants to control its own serialization using ISerializable
. Ensure that your application has the using System.Runtime.Serialization;
added to the using
statements.
How to do it...
- Create a class called
Vehicle
. You will notice that this class...