Aquí va un ejemplo mas de como hacer serialización binaria, con propósitos de transmitir objetos, o almacenarlos en una Base de Datos
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Public Class SerializacionBinaria
Public Shared Function Serializa(ByVal Obj As Object) As Byte()
Dim MS As New MemoryStream
Dim BF As New BinaryFormatter
MS.Position = 0
Dim Buffer() As Byte
Try
BF.Serialize(MS, Obj)
Buffer = MS.ToArray
Catch ex As Exception
Throw ex
Finally
MS.Flush()
MS.Close()
End Try
Return Buffer
End Function
Public Shared Function Deserializa(ByVal SerializedData() As Byte) As Object
Dim MS As MemoryStream = FuncsAndUtils.ArrayToStream(SerializedData)
Dim BF As New BinaryFormatter
MS.Position = 0
Dim odeserialized As Object
Try
odeserialized = BF.Deserialize(MS)
Catch ex As Exception
Throw ex
Finally
MS.Flush()
MS.Close()
End Try
Return odeserialized
End Function
Public Shared Function Deserializa(ByVal SerializedData As Stream) As Object
Dim BF As New BinaryFormatter
Dim odeserialized As Object
Try
odeserialized = BF.Deserialize(SerializedData)
Catch ex As Exception
Throw ex
End Try
Return odeserialized
End Function
End Class