示例#1
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());
        }
示例#2
0
            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);
            }
示例#3
0
            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());
            }
示例#4
0
 public void ReadFrom(IAmReadable other)
 {
     ReadFrom(ReadFormatter.ReadFormatted(other));
 }
示例#5
0
 public void Write(IAmReadable source)
 {
     Write(Read(source));
 }
示例#6
0
        /// <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);
        }