Friday, December 26, 2008

.Net Serialization / De-Serialization

<serializable()>_
Public Class test
Private Id As Long
Private Name As String

End Class

Sub serialize_data()
Dim obj As New test
Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim stream As IO.Stream = New System.IO.FileStream("MyFile.bin", System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None)
formatter.Serialize(stream, obj)
stream.Close()
End Sub

Sub deserialize_data()
Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim stream As System.IO.Stream = New System.IO.FileStream("MyFile.bin", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
Dim obj As test = CType(formatter.Deserialize(stream), test)
stream.Close()
End Sub

No comments:

Post a Comment