Using System. runtime. serialization. formatters. Binary;
Public Class Serializationunit
{
/// <Summary>
/// Serialize an object into a byte array
/// </Summary>
Public Static Byte [] Serializeobject ( Object OBJ)
{
If (OBJ = Null )
Return Null ;
Memorystream MS = New Memorystream ();
Binaryformatter formatter = New Binaryformatter ();
Formatter. serialize (MS, OBJ );
Ms. Position = 0 ;
Byte [] Bytes = New Byte [Ms. Length];
Ms. Read (bytes, 0 , Bytes. Length );
Ms. Close ();
Return Bytes;
}
/// <Summary>
/// Deserializes byte arrays into objects
/// </Summary>
Public Static Object Deserializeobject ( Byte [] Bytes)
{
Object OBJ = Null ;
If (Bytes = Null )
Return OBJ;
Memorystream MS = New Memorystream (bytes );
Ms. Position = 0 ;
Binaryformatter formatter = New Binaryformatter ();
OBJ = Formatter. deserialize (MS );
Ms. Close ();
Return OBJ;
}
}