It's not as simple as that: it depends on what you class contains. If it has references to other classes (as it probably does) then you need to include those as well.
To convert it to a raw byte array is pretty simple:
private static byte[] ToByteArraySimple(object o)
{
int length = Marshal.SizeOf(o);
byte[] bytes = new byte[length];
IntPtr start = Marshal.AllocHGlobal(length);
Marshal.StructureToPtr(o, start, false);
Marshal.Copy(start, bytes, 0, length);
Marshal.FreeHGlobal(start);
return bytes;
}
But to be safe, mark your class with the Serializable attribute (and all referenced classes) and use a Binary Formatter:
private static byte[] ToByteArrayBetter(object o)
{
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream())
{
formatter.Serialize(stream, o);
return stream.ToArray();
}
}