|
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
//Serializes and deserializes an object,
//or an entire graph of connected objects, in binary format.
namespace Common
{
public class Serialization
{
private BinaryFormatter formatter;
private MemoryStream memStream;
public Serialization()
{
try
{
formatter = new BinaryFormatter();
}
catch (Exception e)
{
}
}
public Boolean Serialize(ref Message message,ref byte[] data)
{
// Serializes an object to the memory stream.
Boolean retVal = true;
try
{
memStream = new MemoryStream();
formatter.Serialize(memStream, message);
data = memStream.GetBuffer();
}
catch (Exception e)
{
retVal = false;
}
return retVal;
}
public Boolean Deserialize(ref byte[] data, ref Message message)
{
//Deserializes a memory stream into an object graph.
Boolean retVal = true;
try
{
memStream = new MemoryStream(data);
message = (Message)formatter.Deserialize(memStream);
}
catch (Exception e)
{
retVal = false;
}
return retVal;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.