// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public class X { private int v; public int V { get { return v; } set { v = value; } } } class MainClass { public static byte[] Object2ByteArray(Object o) { MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, o); return ms.ToArray(); } public static object ByteArray2Object(byte[] theByteArray) { MemoryStream ms = new MemoryStream(theByteArray); BinaryFormatter bf = new BinaryFormatter(); ms.Position = 0; return bf.Deserialize(ms); } public static void Main(string[] args) { X x1 = new X(); x1.V = 123; Console.WriteLine(x1.V); byte[] b = Object2ByteArray(x1); X x2 = (X)ByteArray2Object(b); Console.WriteLine(x2.V); } }