public static T Clone <T>(T obj)
        {
            var memoryStream = new MemoryStream();
            var writer       = new BinarySerializationWriter(memoryStream);

            writer.SerializeExtended(obj, ArchiveMode.Serialize);
            writer.Flush();

            var result = default(T);

            memoryStream.Seek(0, SeekOrigin.Begin);
            var reader = new BinarySerializationReader(memoryStream);

            reader.SerializeExtended(ref result, ArchiveMode.Deserialize);
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Reads an object instance from the specified byte buffer.
        /// </summary>
        /// <typeparam name="T">Type of the object to read</typeparam>
        /// <param name="buffer">The byte buffer to read the object instance.</param>
        /// <returns>An object instance of type T.</returns>
        public static T Read <T>([NotNull] byte[] buffer)
        {
            var reader = new BinarySerializationReader(new MemoryStream(buffer));

            return(reader.Read <T>());
        }
Beispiel #3
0
        /// <summary>
        /// Reads an object instance from the specified stream.
        /// </summary>
        /// <typeparam name="T">Type of the object to read</typeparam>
        /// <param name="stream">The stream to read the object instance.</param>
        /// <returns>An object instance of type T.</returns>
        public static T Read <T>([NotNull] Stream stream)
        {
            var reader = new BinarySerializationReader(stream);

            return(reader.Read <T>());
        }