public void Serialize(Stream serializationStream, object graph)
 {
     int key;
     if (!m_ByType.TryGetValue(graph.GetType(), out key))
         throw new SerializationException(graph.GetType() + " has not been registered with the serializer");
     ICustomBinarySerializable c = (ICustomBinarySerializable) graph; //this will always work due to generic constraint on the Register
     m_WriteStream.Seek(0L, SeekOrigin.Begin);
     m_Writer.Write((int) key);
     c.WriteDataTo(m_Writer);
     IntToBytes length = new IntToBytes((int) m_WriteStream.Position);
     serializationStream.WriteByte(length.b0);
     serializationStream.WriteByte(length.b1);
     serializationStream.WriteByte(length.b2);
     serializationStream.WriteByte(length.b3);
     serializationStream.Write(m_WriteStream.GetBuffer(), 0, (int) m_WriteStream.Position);
 }
 public object Deserialize(Stream serializationStream)
     {
         if(serializationStream.Read(m_LengthBuffer, 0, 4) != 4)
             throw new SerializationException("Could not read length from the stream.");
         IntToBytes length = new IntToBytes(m_LengthBuffer[0], m_LengthBuffer[1], m_LengthBuffer[2], m_LengthBuffer[3]);
         //TODO make this support partial reads from stream
         if(serializationStream.Read(m_CopyBuffer, 0, length.i32) != length.i32) 
             throw new SerializationException("Could not read " + length + " bytes from the stream.");
         m_ReadStream.Seek(0L, SeekOrigin.Begin);
         m_ReadStream.Write(m_CopyBuffer, 0, length.i32);
         m_ReadStream.Seek(0L, SeekOrigin.Begin);
         int typeid = m_Reader.ReadInt32();
         Type t;
         if(!m_ById.TryGetValue(typeid, out t))
             throw new SerializationException("TypeId " + typeid + " is not a registerred type id");
         object obj = FormatterServices.GetUninitializedObject(t);
         ICustomBinarySerializable deserialize = (ICustomBinarySerializable) obj;
         deserialize.SetDataFrom(m_Reader);
         if(m_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;
     }
        public  byte[] Serialize(object theObject)
        {
            using (var memoryStream = new MemoryStream())
            {
                int key;
                if (!m_ByType.TryGetValue(theObject.GetType(), out key))
                    throw new SerializationException(theObject.GetType() + " has not been registered with the serializer");

                Internal.IntToBytes itob = new IntToBytes(key);
                memoryStream.WriteByte(itob.b0);
                memoryStream.WriteByte(itob.b1);
                memoryStream.WriteByte(itob.b2);
                memoryStream.WriteByte(itob.b3);


                IFormatter form;
                if (!m_Formatters.TryGetValue(theObject.GetType(), out form))
                    throw new SerializationException(theObject.GetType() + " has not been registered with the serializer");
                form.Serialize(memoryStream,theObject);
                //ProtoBuf.Serializer.Serialize(memoryStream, theObject);
                //_ctx.Serialize(memoryStream, theObject);
                return memoryStream.ToArray();
            }
        }
        /*
        Castle.Windsor.IWindsorContainer _container;
        public class Caller
        {
            public string Typename;
            public Type Theclass;
        }

        private List<Caller> _mCallers = new List<Caller>();


                public void RegisterAll(Castle.Windsor.IWindsorContainer container)
                {
                    this._container = container;
                    foreach (var v in AppDomain.CurrentDomain.GetAssemblies())
                        AutoRegister(v);
            
                }
        */
        public object Deserialize(byte[] bytes)
        {
            using (var memoryStream = new MemoryStream(bytes))
            {
                //Read type

                IntToBytes itob = new IntToBytes((byte)memoryStream.ReadByte(),
                    (byte)memoryStream.ReadByte(),
                    (byte)memoryStream.ReadByte(),
                    (byte)memoryStream.ReadByte());
                Type t;
                if (!m_ById.TryGetValue(itob.i32, out t))
                    throw new SerializationException(itob.i32 + " has not been registered with the serializer");

                IFormatter form;
                if (!m_Formatters.TryGetValue(t, out form))
                    throw new SerializationException(t.GetType() + " has not been registered with the serializer");

                return form.Deserialize(memoryStream);
                //object obj = FormatterServices.GetUninitializedObject(t);
                //return ProtoBuf.Serializer.Merge(memoryStream, obj);
                //IEvent
                //return _ctx.Deserialize(memoryStream);
            }

        }