public byte[] SerializeToArray(object o) { if (o == null) return null; BinaryFormatter bf = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream()) { bf.Serialize(ms, o); return ms.ToArray(); } }