// 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 Ser { public static byte[] Object2ByteArray(T o) { MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, o); return ms.ToArray(); } public static string Object2String(T o) { return Convert.ToBase64String(Object2ByteArray(o)); } public static T ByteArray2Object(byte[] b) { MemoryStream ms = new MemoryStream(b); BinaryFormatter bf = new BinaryFormatter(); ms.Position = 0; return (T)bf.Deserialize(ms); } public static T String2Object(string s) { return ByteArray2Object(Convert.FromBase64String(s)); } } public class MainClass { public static void Main(string[] args) { DateTime dt1 = DateTime.Now; Console.WriteLine(dt1); DateTime dt2 = Ser.String2Object(Ser.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 = Ser>.String2Object(Ser>.Object2String(lst1)); foreach(string s in lst2) Console.WriteLine(s); } } }