public static T Deserialize<T, S>(S stream) where S : Stream where T : class, new() { using (stream) { BinaryFormatter formatter = new BinaryFormatter(); return (T)formatter.Deserialize(stream); } } public static void Serialize<T, S>(T obj, S stream) where S : Stream where T : class, new() { using (stream) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); } }