예제 #1
0
        object ReadValueContents(Type knownType)
        {
            int typeIndex = _binaryReader.ReadInt32();

            if (typeIndex == -1)
            {
                return(null);
            }

            Type type;

            if (typeIndex == -2)
            {
                type = knownType;
            }
            else
            {
                type = SerializerRegistry.ReadSerializedTypeId(typeIndex, _binaryReader);
            }

            var serializer = SerializerRegistry.GetSerializer(type);

            object value;

            serializer.DeserializeConstructor(out value, _version, this);
            serializer.DeserializeContents(ref value, _version, this);
            serializer.DeserializeBacklinks(ref value, _version, this);
            return(value);
        }
예제 #2
0
        public object ReadFromBinary()
        {
            _version = _binaryReader.ReadInt32();

            List <ReadInfo> rootReferencesInOrder = new List <ReadInfo>();

            while (true)
            {
                long id = _binaryReader.ReadInt64();
                if (id == 0)
                {
                    break;
                }

                var info = new ReadInfo();
                info.Id = id;
                info.ConstructorPosition = _binaryReader.ReadInt64();
                info.ContentsPosition    = _binaryReader.ReadInt64();
                info.BacklinkPosition    = _binaryReader.ReadInt64();
                info.SerializedType      = SerializerRegistry.ReadSerializedTypeId(_binaryReader);
                info.Serializer          = SerializerRegistry.GetSerializer(info.SerializedType);

                _rootReferences.Add(id, info);
                rootReferencesInOrder.Add(info);
            }

            long rootId = _binaryReader.ReadInt64();
            long constructorSeekOffset = _binaryReader.ReadInt64();
            long contentsSeekOffset    = _binaryReader.ReadInt64();
            long backlinkSeekOffset    = _binaryReader.ReadInt64();

            // endSeekOffset
            _binaryReader.ReadInt64();

            foreach (var info in rootReferencesInOrder)
            {
                info.ConstructorPosition += constructorSeekOffset;
                info.ContentsPosition    += contentsSeekOffset;
                info.BacklinkPosition    += backlinkSeekOffset;
            }

            foreach (var info in rootReferencesInOrder)
            {
                DeserializeConstructor(info);
                DeserializeContents(info, false);
            }

            _binaryReader.BaseStream.Seek(backlinkSeekOffset, SeekOrigin.Begin);

            foreach (var info in rootReferencesInOrder)
            {
                info.Serializer.DeserializeBacklinks(ref info.Value, _version, this);
            }

            foreach (var info in _rootReferences.Values)
            {
                info.Serializer.DeserializationComplete(ref info.Value, _version);
            }

            return(_rootReferences[rootId].Value);
        }
예제 #3
0
        public object ReadPrimitive(Type knownType)
        {
            Assert.IsNotNull(knownType);

            if (knownType == typeof(bool))
            {
                return(_binaryReader.ReadBoolean());
            }
            else if (knownType == typeof(byte))
            {
                return(_binaryReader.ReadByte());
            }
            else if (knownType == typeof(short))
            {
                return(_binaryReader.ReadInt16());
            }
            else if (knownType == typeof(ushort))
            {
                return(_binaryReader.ReadUInt16());
            }
            else if (knownType == typeof(int))
            {
                return(_binaryReader.ReadInt32());
            }
            else if (knownType == typeof(uint))
            {
                return(_binaryReader.ReadUInt32());
            }
            else if (knownType == typeof(long))
            {
                return(_binaryReader.ReadInt64());
            }
            else if (knownType == typeof(ulong))
            {
                return(_binaryReader.ReadUInt64());
            }
            else if (knownType == typeof(float))
            {
                return(_binaryReader.ReadSingle());
            }
            else if (knownType == typeof(double))
            {
                return(_binaryReader.ReadDouble());
            }
            else if (knownType == typeof(decimal))
            {
                return(_binaryReader.ReadDecimal());
            }
            else if (knownType == typeof(char))
            {
                return(_binaryReader.ReadChar());
            }
            else if (knownType == typeof(string))
            {
                return(_binaryReader.ReadString());
            }
            else if (knownType == typeof(byte[]))
            {
                var length = _binaryReader.ReadInt32();
                return(_binaryReader.ReadBytes(length));
            }
            else if (knownType == typeof(Type))
            {
                return(SerializerRegistry.ReadSerializedTypeId(_binaryReader));
            }
            else if (knownType.IsEnum())
            {
                return(Enum.ToObject(knownType, ReadPrimitive(Enum.GetUnderlyingType(knownType))));
            }

            throw Assert.CreateException(
                      "unhandled primative type '{0}'", knownType.Name());
        }