/// <summary> /// Initialize an object from a stream, then returns a snapshot memento of it /// </summary> /// <param name="self"></param> /// <param name="stream"></param> /// <returns></returns> public static IMemento GetMemento(this ICanRead self, Stream stream) { IMemento memento = new StreamMemento(stream, new FormatterStrategy()); memento.ReadFrom(self); return(memento); }
/// <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()); }
/// <summary> /// Save an object to a stream /// </summary> /// <param name="self"></param> /// <param name="stream"></param> public static void WriteTo(this ICanRead self, Stream stream) { IMemento memento = new StreamMemento(stream, new FormatterStrategy()); memento.ReadFrom(self); }