Ejemplo n.º 1
0
        private object?LoadObjectV2(int dataOffset)
        {
            Debug.Assert(System.Threading.Monitor.IsEntered(reader));
            reader.Seek(dataSectionPosition + dataOffset, SeekOrigin.Begin);
            var typeCode = (ResourceTypeCode)reader.Read7BitEncodedInt();

            switch (typeCode)
            {
            case ResourceTypeCode.Null:
                return(null);

            case ResourceTypeCode.String:
                return(reader.ReadString());

            case ResourceTypeCode.Boolean:
                return(reader.ReadBoolean());

            case ResourceTypeCode.Char:
                return((char)reader.ReadUInt16());

            case ResourceTypeCode.Byte:
                return(reader.ReadByte());

            case ResourceTypeCode.SByte:
                return(reader.ReadSByte());

            case ResourceTypeCode.Int16:
                return(reader.ReadInt16());

            case ResourceTypeCode.UInt16:
                return(reader.ReadUInt16());

            case ResourceTypeCode.Int32:
                return(reader.ReadInt32());

            case ResourceTypeCode.UInt32:
                return(reader.ReadUInt32());

            case ResourceTypeCode.Int64:
                return(reader.ReadInt64());

            case ResourceTypeCode.UInt64:
                return(reader.ReadUInt64());

            case ResourceTypeCode.Single:
                return(reader.ReadSingle());

            case ResourceTypeCode.Double:
                return(reader.ReadDouble());

            case ResourceTypeCode.Decimal:
                return(reader.ReadDecimal());

            case ResourceTypeCode.DateTime:
                // Use DateTime's ToBinary & FromBinary.
                long data = reader.ReadInt64();
                return(DateTime.FromBinary(data));

            case ResourceTypeCode.TimeSpan:
                long ticks = reader.ReadInt64();
                return(new TimeSpan(ticks));

            // Special types
            case ResourceTypeCode.ByteArray:
            {
                int len = reader.ReadInt32();
                if (len < 0)
                {
                    throw new BadImageFormatException("Resource with negative length");
                }
                return(reader.ReadBytes(len));
            }

            case ResourceTypeCode.Stream:
            {
                int len = reader.ReadInt32();
                if (len < 0)
                {
                    throw new BadImageFormatException("Resource with negative length");
                }
                byte[] bytes = reader.ReadBytes(len);
                return(new MemoryStream(bytes, writable: false));
            }

            default:
                if (typeCode < ResourceTypeCode.StartOfUserTypes)
                {
                    throw new BadImageFormatException("Invalid typeCode");
                }
                return(new ResourceSerializedObject(FindType(typeCode - ResourceTypeCode.StartOfUserTypes), this, reader.BaseStream.Position));
            }
        }