Serialization is the act of saving an object onto a storage medium – a file, a data base field, a buffer in memory and later de-serialization it from the medium to re-create an object instance that can be considered identical to the original one. Serialization is also known as Persistence, however, MSDN documentation defines persistence as saved to a durable medium.
Basic Serialization: .Net framework knows how to serialize all basic data types (numbers, string, arrays etc). A formatter is an object that implements the IFormattter interface (defined in System.Runtime.Serailization). User can create his own formatter by defining a class that implements this interface.
Predefined formatters:
Binary: (System.Runtime.Serialization.Formatter.Binary) persists an object as compact binary formatter.
Example
Dim arr() As Interger = {1,2,3}
Dim fs As FileStream = New FileStream (filename, FileMode.Create)
Dim bf As New BinaryFormatter()
bf.Serialize ( fs, arr)
fs.close ()
Open
Dim fs as FileStream = New FileStream (FileName, FileMode.Open)
Dim Arr () As Integer = CType (bf.Deserialize (fs), Integer ())
For each n As Integer in Arr
Console.Write (n.ToString & “ “ )
Next
Soap: (System.Runtime.Serialize.Formatter.Soap) in human readable XML file format using SOAP specs.
Dim sf As New SoapFormatter (Nohing, ..)
Serialized and non-serialized attributes: A class can be made serializable by setting a flag with serializable attributes.
Class ClassName
…
End Class
For this to work base class must be serializable. Similarly non serializable can be used to make a class nonserializable (Non-Serializable flag). Item that can and should be calculated at runtime, should be non-serializable.
Object Graphs: Object graph is a set of multiple object with reference to one another. Serialization mechanism is smart enough to understand the underlying connections.
Deep Object Cloning:
Custom Serialization: iDeserializationCallBack and ISerializable interfaces allows developers to implement custom serialization.
Formatter Services Helper Class: Expresses a few shared methods that help you build code that serializes and des-serializes an object
Class SuperClass
Implements ISerialziable
Public Sub …
…
…
Add a comment