public static T CloneViaBinaryData <T>(this T dataObjectToClone) { using (var dataStream = new MemoryStream()) { var writer = new BinaryWriter(dataStream); BinaryDataExtensions.Save(dataObjectToClone, writer); dataStream.Seek(0, SeekOrigin.Begin); var reader = new BinaryReader(dataStream); var clonedObject = (T)reader.Create(); return(clonedObject); } }
public void SaveAndLoadRectangleManuallyWithBinaryWriterAndReader() { using (var dataStream = new MemoryStream()) { var writer = new BinaryWriter(dataStream); var data = Rectangle.One; BinaryDataExtensions.Save(data, writer); dataStream.Seek(0, SeekOrigin.Begin); var reader = new BinaryReader(dataStream); data = (Rectangle)reader.Create(); Assert.AreEqual(Rectangle.One, data); } }
public void CannotSaveObjectDataIfNull() { var binaryWriter = new BinaryWriter(new MemoryStream()); Assert.Throws <ArgumentNullException>(() => BinaryDataExtensions.Save(null, binaryWriter)); }