// 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; using System.Collections.Generic; namespace E { public 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 string Object2String(object o) { return Convert.ToBase64String(Object2ByteArray(o)); } public static object ByteArray2Object(byte[] b) { MemoryStream ms = new MemoryStream(b); BinaryFormatter bf = new BinaryFormatter(); ms.Position = 0; return bf.Deserialize(ms); } public static object String2Object(string s) { return ByteArray2Object(Convert.FromBase64String(s)); } public static void Main(string[] args) { DateTime dt1 = DateTime.Now; Console.WriteLine(dt1); DateTime dt2 = (DateTime)String2Object(Object2String(dt1)); Console.WriteLine(dt2); List lst1 = new List(); lst1.Add("A"); lst1.Add("BB"); lst1.Add("CCC"); foreach(string s in lst1) Console.WriteLine(s); List lst2 = (List)String2Object(Object2String(lst1)); foreach(string s in lst2) Console.WriteLine(s); } } }