Beispiel #1
0
        static public T Clone <T>(T source)
        {
            Debug.Assert(typeof(T).IsSerializable);
            IGenericFormatter formatter = new GenericBinaryFormatter();
            Stream            stream    = new MemoryStream();

            using (stream)
            {
                formatter.Serialize(stream, source);
                stream.Seek(0, SeekOrigin.Begin);
                T clone = formatter.Deserialize <T>(stream);
                return(clone);
            }
        }
        public static T Clone <T>(T source)
        {
            Debug.Assert(typeof(T).IsSerializable);
            if (!typeof(T).IsSerializable)
            {
                throw new SerializationException(ExceptionMessages.ObjectNoSerializable);
            }

            T result;
            IGenericFormatter formatter = new GenericBinaryFormatter();

            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, source);
                stream.Seek(0, SeekOrigin.Begin);
                result = formatter.Deserialize <T>(stream);
            }
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Writes the Content of the Log to the specified Stream in binary format.
        /// </summary>
        /// <param name="stream">The Stream where the content of the log will be written.</param>
        public void WriteTo(Stream stream)
        {
            if (stream == null) throw new ArgumentNullException("stream");

            if (Entries.Any())
            {
                stream.Seek((int)stream.Position, SeekOrigin.Begin);
                stream.Position = (int)stream.Position;

                GenericBinaryFormatter formatter = new GenericBinaryFormatter();
                byte[] serialized = formatter.Serialize<LogEntries>(Entries);
                if (serialized != null && serialized.Any())
                {
                    stream.Write(serialized, 0, serialized.Length);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Loads the content of the Log from binary serialized entries.
        /// </summary>
        /// <param name="binaryContent">The entries serialized in binary format.</param>
        public void LoadBinary(BinaryContent binaryContent)
        {
            if (binaryContent == null)
            { throw new ArgumentNullException("binaryContent"); }

            GenericBinaryFormatter formatter = new GenericBinaryFormatter();
            using (MemoryStream stream = new MemoryStream(binaryContent))
            {
                while (stream.Position < binaryContent.Size)
                {
                    var logEntries = formatter.Deserialize<LogEntries>(stream);
                    Entries.AddRange(logEntries);
                }
            }
        }