CreateWriter() public abstract method

public abstract CreateWriter ( Stream stream ) : ObjectWriter
stream Stream
return ObjectWriter
        public static void SerializePersonTest(Person person, ObjectReaderWriterFactory factory)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                SerializePerson(person, factory.CreateWriter(stream));

                byte[] serialized = stream.ToArray();

#if DEBUG
                string writeDump = serialized.ToAsciiDumpString();
                Debug.WriteLine(writeDump);
#endif

                Test.Output("{0} writer wrote {1} bytes", factory.Name, serialized.Length);

                // Deserializer 1

                stream.Position = 0;
                DeserializePerson1(factory.CreateReader(stream));

                // Deserializer 2

                stream.Position = 0;
                DeserializePerson2(factory.CreateReader(stream));
            }
        }
        private void TestComplexObjectSerialization(
            ObjectReaderWriterFactory factory)
        {
            const int BufferSizeInBytes = 64 * 1024;

            Stopwatch writeStopWatch = new Stopwatch();
            Stopwatch readStopWatch = new Stopwatch();

            ComplexObject writeObject = ComplexObject.Create();

            byte[] serialized;

            var writerInspector = new TestSerializationInspector();

            // Serialize the test object to a byte array
            using (var stream = new MemoryStream(BufferSizeInBytes))
            {
                var writer = factory.CreateWriter(stream);

                writer.Inspector = writerInspector;

                writeStopWatch.Start();

                ComplexObject.WriteTo(writer, writeObject);

                writeStopWatch.Stop();

                serialized = stream.ToArray();
            }

            //string writeDump = serialized.ToAsciiDumpString();
            //Debug.WriteLine(writeDump);

            Test.Output("{0} writer wrote {1} bytes in {2}ms", factory.Name, serialized.Length, writeStopWatch.ElapsedMilliseconds);

            // Validate inspector stream
            byte[] writerInspectorBytes = writerInspector.ToByteArray();
            if (!ArrayComparer<byte>.Equals(writerInspectorBytes, serialized))
            {
                throw new InvalidOperationException("ISerializationInspector byte stream does not match the serialized byte stream.");
            }

            var readerInspector = new TestSerializationInspector();

            ComplexObject readObject = null;

            // Deserialize the test object from a byte array
            using (var stream = new MemoryStream(serialized))
            {
                var reader = factory.CreateReader(stream);

                reader.Inspector = readerInspector;

                readStopWatch.Start();

                readObject = ComplexObject.ReadFrom(reader);

                readStopWatch.Stop();
            }

            Test.Output("{0} reader read {1} bytes in {2}ms", factory.Name, serialized.Length, readStopWatch.ElapsedMilliseconds);

            // Serialize the deserialized object for comparison
            byte[] deserialized;
            using (var stream = new MemoryStream(BufferSizeInBytes))
            {
                var writer = factory.CreateWriter(stream);

                ComplexObject.WriteTo(writer, readObject);

                deserialized = stream.ToArray();
            }

            // Compare serialized values
            if (!ArrayComparer<byte>.Equals(serialized, deserialized))
            {
                throw new Exception("Serialized and deserialized streams do not match.");
            }

            // Compare objects
            writeObject.VerifyIsEqual(readObject);

            // Validate inspector stream
            byte[] readerInspectorBytes = readerInspector.ToByteArray();
            if (!ArrayComparer<byte>.Equals(readerInspectorBytes, serialized))
            {
                throw new InvalidOperationException("ISerializationInspector byte stream does not match the serialized byte stream.");
            }
        }
        public void TestComplexObjectPerformance(
            ObjectReaderWriterFactory factory)
        {
            const int WarmupIterations = 10;
            const int Iterations = 100;

            Stopwatch writeStopWatch = new Stopwatch();

            byte[] buffer = new byte[128 * 1024];
            byte[] serialized;

            ComplexObject writeObject = ComplexObject.Create();

            // Serialize the test object to a byte array
            using (var stream = new MemoryStream(buffer))
            {
                IObjectWriter writer;

                GC.Collect(3, GCCollectionMode.Forced, true);

                // Warm-up
                for (int i = 0; i < WarmupIterations; i += 1)
                {
                    stream.SetLength(0);
                    writer = factory.CreateWriter(stream);
                    ComplexObject.WriteTo(writer, writeObject);
                }

                writeStopWatch.Start();

                for (int i = 0; i < Iterations; i += 1)
                {
                    stream.SetLength(0);
                    writer = factory.CreateWriter(stream);
                    ComplexObject.WriteTo(writer, writeObject);
                }

                writeStopWatch.Stop();

                serialized = stream.ToArray();
            }

            Test.Output("{0} writer serialized {1} complex objects ({2:#,###} bytes each) in {3} ms ({4} ticks @ {5}). Average is {6} ticks per object.",
                factory.Name, Iterations, serialized.Length, writeStopWatch.Elapsed.TotalMilliseconds, writeStopWatch.ElapsedTicks, Stopwatch.Frequency, writeStopWatch.ElapsedTicks / Iterations);

            Stopwatch readStopWatch = new Stopwatch();

            ComplexObject readObject = null;

            // Deserialize the test object from a byte array
            using (var stream = new MemoryStream(serialized))
            {
                IObjectReader reader;

                // Warm-up
                for (int i = 0; i < WarmupIterations; i += 1)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    reader = factory.CreateReader(stream);
                    readObject = ComplexObject.ReadFrom(reader);
                }

                readStopWatch.Start();

                for (int i = 0; i < Iterations; i += 1)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    reader = factory.CreateReader(stream);
                    readObject = ComplexObject.ReadFrom(reader);
                }

                readStopWatch.Stop();
            }

            Test.Output("{0} reader deserialized {1} complex objects ({2:#,###} bytes each) in {3} ms ({4} ticks @ {5}). Average is {6} ticks per object.",
                factory.Name, Iterations, serialized.Length, readStopWatch.Elapsed.TotalMilliseconds, readStopWatch.ElapsedTicks, Stopwatch.Frequency, readStopWatch.ElapsedTicks / Iterations);

            // Compare objects
            writeObject.VerifyIsEqual(readObject);
        }