private object ReadStructTypeValue(RSBinaryReader _binaryReader, out Type _objectType, object _object)
        {
            // Get object type
            UInt32 _typeID = _binaryReader.ReadUInt32();

            _objectType = TypeMetadata.GetType(_typeID);

            // Read object refernece
            Dictionary <string, RuntimeSerializationEntry> _initValuesContainer = new Dictionary <string, RuntimeSerializationEntry>();
            Dictionary <string, RuntimeSerializationEntry> _serValuesContainer  = new Dictionary <string, RuntimeSerializationEntry>();
            RuntimeSerializationInfo _serializationInfo = new RuntimeSerializationInfo(_objectType, _initValuesContainer, _serValuesContainer);

            // Read initializers and properties
            ReadSerializationContainer(_binaryReader, ref _initValuesContainer);
            ReadSerializationContainer(_binaryReader, ref _serValuesContainer);

            if (_object == null)
            {
                _object = CreateInstance(_binaryReader, _serializationInfo);
            }

            // Set object value
            _object = SetObjectData(_object, _serializationInfo);

            // Trigger deserialization callback
            if (typeof(IRuntimeSerializationCallback).IsAssignableFrom(_objectType))
            {
                ((IRuntimeSerializationCallback)_object).OnAfterRuntimeDeserialize();
            }

            return(_object);
        }
        private object ReadPrimitiveTypeValue(RSBinaryReader _binaryReader, out Type _objectType)
        {
            // Get object type
            UInt32 _typeID = _binaryReader.ReadUInt32();

            _objectType = TypeMetadata.GetType(_typeID);

            // Get object value
            return(_binaryReader.ReadPrimitiveValue((TypeCode)_typeID));
        }
        internal void ReadTypeMetaData(RSBinaryReader _binaryReader, BinaryElement _binaryElement)
        {
            // Read assembly info
            if (_binaryElement == BinaryElement.ASSEMBLY)
            {
                UInt32   _assemblyID       = _binaryReader.ReadUInt32();
                string   _assemblyFullName = _binaryReader.ReadString();
                Assembly _assembly         = Assembly.Load(_assemblyFullName);

                if (_assembly == null)
                {
                    throw new Exception(string.Format("[RS] Couldnt load assembly with name={0}.", _assemblyFullName));
                }

                // Add assembly info to cache
                m_cachedAssemblies.Add(_assembly, _assemblyID);

                // Read next element
                _binaryElement = _binaryReader.ReadBinaryElement();
            }

            // Read type info
            if (_binaryElement == BinaryElement.TYPE)
            {
                UInt32   _assemblyID   = _binaryReader.ReadUInt32();
                UInt32   _typeID       = _binaryReader.ReadUInt32();
                string   _typeFullName = _binaryReader.ReadString();
                Assembly _assembly     = GetAssembly(_assemblyID);

                if (_assembly == null)
                {
                    throw new Exception(string.Format("[RS] Assembly with ID={0} couldnt be found.", _assemblyID));
                }

                Type _newType = _assembly.GetType(_typeFullName, true);

                // Add type info to cache
                m_cachedObjectTypes.Add(_newType, _typeID);
            }
        }
        private object ReadObjectReferenceValue(RSBinaryReader _binaryReader, out Type _objectType)
        {
            UInt32 _objectReferenceID = _binaryReader.ReadUInt32();
            object _object;

            // Find object using reference ID
            Dictionary <object, UInt32> .Enumerator _dictEnumerator = ObjectReferenceCache.GetEnumerator();

            while (_dictEnumerator.MoveNext())
            {
                KeyValuePair <object, UInt32> _keyValuePair = _dictEnumerator.Current;
                UInt32 _currentReferenceID = _keyValuePair.Value;

                if (_currentReferenceID == _objectReferenceID)
                {
                    _object     = _keyValuePair.Key;
                    _objectType = _object.GetType();

                    return(_object);
                }
            }

            throw new Exception(string.Format("[RS] Object Reference not found for ID={0}.", _objectReferenceID));
        }
        private object ReadClassTypeValue(RSBinaryReader _binaryReader, out Type _objectType, object _object)
        {
            // Read properties
            UInt32		_typeID					= _binaryReader.ReadUInt32();
            UInt32		_objectReferenceID		= _binaryReader.ReadUInt32();

            // Get object type
            _objectType							= TypeMetadata.GetType(_typeID);

            // Read object refernece
            Dictionary<string, RuntimeSerializationEntry>	_initValuesContainer	= new Dictionary<string, RuntimeSerializationEntry>();
            Dictionary<string, RuntimeSerializationEntry>	_serValuesContainer		= new Dictionary<string, RuntimeSerializationEntry>();
            RuntimeSerializationInfo						_serializationInfo		= new RuntimeSerializationInfo(_objectType, _initValuesContainer, _serValuesContainer);

            // Read initializers and create instance, if required
            ReadSerializationContainer(_binaryReader, ref _initValuesContainer);

            if (_object == null)
                _object							= CreateInstance(_binaryReader, _serializationInfo);

            ObjectReferenceCache.Add(_object, _objectReferenceID);

            // Read properties
            ReadSerializationContainer(_binaryReader, ref _serValuesContainer);

            // Set object value
            _object								= SetObjectData(_object, _serializationInfo);

            // Trigger deserialization callback
            if (typeof(IRuntimeSerializationCallback).IsAssignableFrom(_objectType))
                ((IRuntimeSerializationCallback)_object).OnAfterRuntimeDeserialize();

            return _object;
        }
        private object ReadPrimitiveTypeValue(RSBinaryReader _binaryReader, out Type _objectType)
        {
            // Get object type
            UInt32	_typeID				= _binaryReader.ReadUInt32();
            _objectType					= TypeMetadata.GetType(_typeID);

            // Get object value
            return _binaryReader.ReadPrimitiveValue((TypeCode)_typeID);
        }
        private object ReadObjectReferenceValue(RSBinaryReader _binaryReader, out Type _objectType)
        {
            UInt32 	_objectReferenceID								= _binaryReader.ReadUInt32();
            object 	_object;

            // Find object using reference ID
            Dictionary<object, UInt32>.Enumerator _dictEnumerator	= ObjectReferenceCache.GetEnumerator();

            while (_dictEnumerator.MoveNext())
            {
                KeyValuePair<object, UInt32> 	_keyValuePair		= _dictEnumerator.Current;
                UInt32 							_currentReferenceID	= _keyValuePair.Value;

                if (_currentReferenceID == _objectReferenceID)
                {
                    _object											= _keyValuePair.Key;
                    _objectType										= _object.GetType();

                    return _object;
                }
            }

            throw new Exception(string.Format("[RS] Object Reference not found for ID={0}.", _objectReferenceID));
        }
        internal void ReadTypeMetaData(RSBinaryReader _binaryReader, BinaryElement _binaryElement)
        {
            // Read assembly info
            if (_binaryElement == BinaryElement.ASSEMBLY)
            {
                UInt32 		_assemblyID			= _binaryReader.ReadUInt32();
                string 		_assemblyFullName	= _binaryReader.ReadString();
                Assembly 	_assembly			= Assembly.Load(_assemblyFullName);

                if (_assembly == null)
                    throw new Exception(string.Format("[RS] Couldnt load assembly with name={0}.", _assemblyFullName));

                // Add assembly info to cache
                m_cachedAssemblies.Add(_assembly, _assemblyID);

                // Read next element
                _binaryElement					= _binaryReader.ReadBinaryElement();
            }

            // Read type info
            if (_binaryElement == BinaryElement.TYPE)
            {
                UInt32 		_assemblyID			= _binaryReader.ReadUInt32();
                UInt32 		_typeID				= _binaryReader.ReadUInt32();
                string 		_typeFullName		= _binaryReader.ReadString();
                Assembly 	_assembly			= GetAssembly(_assemblyID);

                if (_assembly == null)
                    throw new Exception(string.Format("[RS] Assembly with ID={0} couldnt be found.", _assemblyID));

                Type 		_newType			= _assembly.GetType(_typeFullName, true);

                // Add type info to cache
                m_cachedObjectTypes.Add(_newType, _typeID);
            }
        }