public static void WriteImaginaryObject(ImaginaryObject imaginaryObject, BinaryWriter writer, bool writeImaginaryObjectUniqueIdentifier = true) { writer.Write(writeImaginaryObjectUniqueIdentifier); var imaginaryObjectUniqueIdentifier = 0; if (writeImaginaryObjectUniqueIdentifier) { imaginaryObjectUniqueIdentifier = totalWrittenImaginaryObjects; imaginaryObjectUniqueIdentifier += imaginaryObject.GetHashCode(); imaginaryObjectUniqueIdentifier += new Random().Next(imaginaryObjectUniqueIdentifier, int.MaxValue); writer.Write(imaginaryObjectUniqueIdentifier); } //try //{ WriteImaginaryObjectType(imaginaryObject.GetType(), writer); imaginaryObject.WriteConstructionInfo(writer); //} //catch (Exception) //{ // // Revert position to where the writer was before. // WriteImaginaryObject(new CorruptedImaginaryObject(), writer); //} if (writeImaginaryObjectUniqueIdentifier) { writer.Write(imaginaryObjectUniqueIdentifier); } totalWrittenImaginaryObjects++; }
public static void SaveToFile(string path, ImaginaryObject toStore) { var settings = new XmlWriterSettings { Indent = true }; Utilities.CreateEmptyFile(path); using (var writerStream = XmlWriter.Create(path, settings)) { new DataContractSerializer(toStore.GetType()).WriteObject(writerStream, toStore); } }