internal void WriteTypeMetadata(RSBinaryWriter _binaryWriter, Type _type, UInt32 _typeID)
        {
            if (_type == null)
            {
                throw new NullReferenceException("[RS] Serialize type metadata failed. Type is null.");
            }

            // Get assembly info
            bool     _newAssembly;
            UInt32   _assemblyID;
            Assembly _assembly = _type.Assembly;

            RegisterAssembly(_assembly, out _assemblyID, out _newAssembly);

            // Write assembly properties (if required)
            if (_newAssembly)
            {
                _binaryWriter.WriteBinaryElement(BinaryElement.ASSEMBLY);
                _binaryWriter.Write(_assemblyID);
                _binaryWriter.Write(_assembly.FullName);
            }

            // Write type properties
            _binaryWriter.WriteBinaryElement(BinaryElement.TYPE);
            _binaryWriter.Write(_assemblyID);
            _binaryWriter.Write(_typeID);
            _binaryWriter.Write(_type.FullName);
        }
 private void WriteStringTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
 {
     // Write string object data
     _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
     _binaryWriter.WriteTypeTag(eTypeTag.STRING);
     _binaryWriter.Write(_object as string);
 }
        private void WritePrimitiveTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
        {
            int _typeID = (int)Type.GetTypeCode(_objectType);

            // Write primitive object data
            _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
            _binaryWriter.WriteTypeTag(eTypeTag.PRIMITIVE);
            _binaryWriter.Write(_typeID);
            _binaryWriter.WritePrimitiveValue(_object, (TypeCode)_typeID);
        }
        private void WriteSerializationContainer(RSBinaryWriter _binaryWriter, Dictionary <string, RuntimeSerializationEntry> _container)
        {
            Dictionary <string, RuntimeSerializationEntry> .Enumerator _enumerator = _container.GetEnumerator();

            // Write count
            _binaryWriter.Write(_container.Count);

            // Write entries
            while (_enumerator.MoveNext())
            {
                RuntimeSerializationEntry _curEntry = _enumerator.Current.Value;

                // Write serialization entry
                _binaryWriter.Write(_curEntry.Name);

                // Write child properties
                WriteObjectValue(_binaryWriter, _curEntry.Value);
            }
        }
        private void WriteClassTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType, UInt32 _objectReferenceID)
        {
            // Register object graph type
            UInt32 _objectTypeID;
            bool   _newType;

            TypeMetadata.RegisterType(_objectType, out _objectTypeID, out _newType);

            if (_newType)
            {
                TypeMetadata.WriteTypeMetadata(_binaryWriter, _objectType, _objectTypeID);
            }

            // Write object properties
            _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
            _binaryWriter.WriteTypeTag(eTypeTag.CLASS);
            _binaryWriter.Write(_objectTypeID);
            _binaryWriter.Write(_objectReferenceID);

            // Write object graph
            WriteObjectGraph(_binaryWriter, _object, _objectType);
        }
        private void WriteObjectReferenceTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType, eTypeTag _typeTag)
        {
            // Check if this object exists in object reference cache
            bool   _firstTime;
            UInt32 _objectReferenceID;

            RegisterObject(_object, out _objectReferenceID, out _firstTime);

            if (_firstTime)
            {
                if (_typeTag == eTypeTag.CLASS)
                {
                    WriteClassTypeValue(_binaryWriter, _object, _objectType, _objectReferenceID);
                    return;
                }
            }
            else
            {
                _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
                _binaryWriter.WriteTypeTag(eTypeTag.OBJECT_REFERENCE);
                _binaryWriter.Write(_objectReferenceID);
                return;
            }
        }
        private void WriteStructTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
        {
            // Register object graph type
            UInt32 	_objectTypeID;
            bool	_newType;

            TypeMetadata.RegisterType(_objectType, out _objectTypeID, out _newType);

            if (_newType)
                TypeMetadata.WriteTypeMetadata(_binaryWriter, _objectType, _objectTypeID);

            // Write object properties
            _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
            _binaryWriter.WriteTypeTag(eTypeTag.STRUCT);
            _binaryWriter.Write(_objectTypeID);

            // Write object graph
            WriteObjectGraph(_binaryWriter, _object, _objectType);
        }
 private void WriteStringTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
 {
     // Write string object data
     _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
     _binaryWriter.WriteTypeTag(eTypeTag.STRING);
     _binaryWriter.Write(_object as string);
 }
        private void WriteSerializationContainer(RSBinaryWriter _binaryWriter, Dictionary<string, RuntimeSerializationEntry> _container)
        {
            Dictionary<string, RuntimeSerializationEntry>.Enumerator _enumerator	 = _container.GetEnumerator();

            // Write count
            _binaryWriter.Write(_container.Count);

            // Write entries
            while (_enumerator.MoveNext())
            {
                RuntimeSerializationEntry 	_curEntry	= _enumerator.Current.Value;

                // Write serialization entry
                _binaryWriter.Write(_curEntry.Name);

                // Write child properties
                WriteObjectValue(_binaryWriter, _curEntry.Value);
            }
        }
        private void WritePrimitiveTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
        {
            int _typeID			= (int)Type.GetTypeCode(_objectType);

            // Write primitive object data
            _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
            _binaryWriter.WriteTypeTag(eTypeTag.PRIMITIVE);
            _binaryWriter.Write(_typeID);
            _binaryWriter.WritePrimitiveValue(_object, (TypeCode)_typeID);
        }
        private void WriteObjectReferenceTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType, eTypeTag _typeTag)
        {
            // Check if this object exists in object reference cache
            bool 	_firstTime;
            UInt32 	_objectReferenceID;

            RegisterObject(_object, out _objectReferenceID, out _firstTime);

            if (_firstTime)
            {
                if (_typeTag == eTypeTag.CLASS)
                {
                    WriteClassTypeValue(_binaryWriter, _object, _objectType, _objectReferenceID);
                    return;
                }
            }
            else
            {
                _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
                _binaryWriter.WriteTypeTag(eTypeTag.OBJECT_REFERENCE);
                _binaryWriter.Write(_objectReferenceID);
                return;
            }
        }
        internal void WriteTypeMetadata(RSBinaryWriter _binaryWriter, Type _type, UInt32 _typeID)
        {
            if (_type == null)
                throw new NullReferenceException("[RS] Serialize type metadata failed. Type is null.");

            // Get assembly info
            bool 		_newAssembly;
            UInt32 		_assemblyID;
            Assembly 	_assembly		= _type.Assembly;

            RegisterAssembly(_assembly, out _assemblyID, out _newAssembly);

            // Write assembly properties (if required)
            if (_newAssembly)
            {
                _binaryWriter.WriteBinaryElement(BinaryElement.ASSEMBLY);
                _binaryWriter.Write(_assemblyID);
                _binaryWriter.Write(_assembly.FullName);
            }

            // Write type properties
            _binaryWriter.WriteBinaryElement(BinaryElement.TYPE);
            _binaryWriter.Write(_assemblyID);
            _binaryWriter.Write(_typeID);
            _binaryWriter.Write(_type.FullName);
        }