private static byte[] ObjectToByteArray(Object obj)
{
if(obj == null)
return null;
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
try {
formatter.Serialize(stream, obj);
byte[] array = stream.ToArray();
return array;
} catch (NotSupportedException) {
// Serialization of certain methods is not supported (namely
// global methods, since they have no representation outside of
// a module scope).
return null;
}
}