/// <summary> /// Returns a snapshot memento of an object, into a byte array /// </summary> /// <param name="self"></param> /// <returns></returns> public static byte[] AsByteArray(this IAmReadable self) { var stream = new MemoryStream(); IMemento memento = new StreamMemento(stream, new FormatterStrategy()); memento.ReadFrom(self); stream.Position = 0; return(stream.ToArray()); }
IEnumerable <object> Read(IAmReadable readable) { ValueType = ValueTypeEnum.Header; yield return(readable.GetType().Name); foreach (var item in readable.ReadParts(this)) { yield return(item); } ValueType = ValueTypeEnum.Footer; yield return(null); }
public IEnumerable <object> ReadFormatted(IAmReadable instance) { yield return(Version); yield return(new ObjectHeader { Version = instance.Version, Name = instance.GetType().Name }); foreach (var item in instance.ReadParts(this)) { yield return(item); } yield return(new ObjectFooter()); }
public void ReadFrom(IAmReadable other) { ReadFrom(ReadFormatter.ReadFormatted(other)); }
public void Write(IAmReadable source) { Write(Read(source)); }
/// <summary> /// Writes a textual representation of an object to a TextWriter /// </summary> /// <param name="self"></param> /// <param name="writer"></param> public static void WriteTo(this IAmReadable self, TextWriter writer) { var formatter = new ObjectTextFormatter(writer, 0); formatter.Write(self); }