예제 #1
0
        /// <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);
        }
예제 #2
0
        /// <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());
        }
예제 #3
0
        /// <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);
        }