public void Serialize(Stream serializationStream, object graph)
        {
            int key;

            if (!_byType.TryGetValue(graph.GetType(), out key))
            {
                throw new SerializationException(graph.GetType() + " has not been registered with the serializer");
            }
            var c = (ICustomBinarySerializable)graph; //this will always work due to generic constraint on the Register

            _writeStream.Seek(0L, SeekOrigin.Begin);
            _writer.Write(key);
            c.WriteDataTo(_writer);
            var length = new IntToBytes((int)_writeStream.Position);

            serializationStream.WriteByte(length.b0);
            serializationStream.WriteByte(length.b1);
            serializationStream.WriteByte(length.b2);
            serializationStream.WriteByte(length.b3);
            serializationStream.Write(_writeStream.GetBuffer(), 0, (int)_writeStream.Position);
        }
        public object Deserialize(Stream serializationStream)
        {
            if (serializationStream.Read(_lengthBuffer, 0, 4) != 4)
            {
                throw new SerializationException("Could not read length from the stream.");
            }
            var length = new IntToBytes(_lengthBuffer[0], _lengthBuffer[1], _lengthBuffer[2], _lengthBuffer[3]);

            _readStream.Seek(0L, SeekOrigin.Begin);
            int read   = 0;
            int toRead = 0;

            while (read < length.i32)
            {
                toRead = length.i32 > _copyBuffer.Length ? _copyBuffer.Length : length.i32;
                var current = serializationStream.Read(_copyBuffer, 0, toRead);
                _readStream.Write(_copyBuffer, 0, current);
                read += current;
            }
            _readStream.Seek(0L, SeekOrigin.Begin);
            int  typeid = _reader.ReadInt32();
            Type t;

            if (!_byId.TryGetValue(typeid, out t))
            {
                throw new SerializationException("TypeId " + typeid + " is not a registerred type id");
            }
            var obj         = FormatterServices.GetUninitializedObject(t);
            var deserialize = (ICustomBinarySerializable)obj;

            deserialize.SetDataFrom(_reader);
            if (_readStream.Position != length.i32)
            {
                throw new SerializationException("object of type " + t + " did not read its entire buffer during deserialization. This is most likely an inbalance between the writes and the reads of the object.");
            }
            return(deserialize);
        }