コード例 #1
0
        private static bool ReadIfPrimitiveOrNull(BinaryReader reader, DObject.TypeCode type, out object value, out DeserializationScope scope)
        {
            if (type != DObject.TypeCode.DObject && !type.HasFlag(DObject.TypeCode.Array))
            {
                scope = null;
                switch (type)
                {
                case DObject.TypeCode.Boolean:
                    value = reader.ReadBoolean();
                    return(true);

                case DObject.TypeCode.Char:
                    value = reader.ReadChar();
                    return(true);

                case DObject.TypeCode.SByte:
                    value = reader.ReadSByte();
                    return(true);

                case DObject.TypeCode.Byte:
                    value = reader.ReadByte();
                    return(true);

                case DObject.TypeCode.Int16:
                    value = reader.ReadInt16();
                    return(true);

                case DObject.TypeCode.UInt16:
                    value = reader.ReadUInt16();
                    return(true);

                case DObject.TypeCode.Int32:
                    value = reader.ReadInt32();
                    return(true);

                case DObject.TypeCode.UInt32:
                    value = reader.ReadUInt32();
                    return(true);

                case DObject.TypeCode.Int64:
                    value = reader.ReadInt64();
                    return(true);

                case DObject.TypeCode.UInt64:
                    value = reader.ReadUInt64();
                    return(true);

                case DObject.TypeCode.Single:
                    value = reader.ReadSingle();
                    return(true);

                case DObject.TypeCode.Double:
                    value = reader.ReadDouble();
                    return(true);

                case DObject.TypeCode.Decimal:
                    value = reader.ReadDecimal();
                    return(true);

                case DObject.TypeCode.DateTime:
                    value = DateTime.FromBinary(reader.ReadInt64());
                    return(true);

                case DObject.TypeCode.TimeSpan:
                    value = TimeSpan.FromTicks(reader.ReadInt64());
                    return(true);

                case DObject.TypeCode.String:
                {
                    byte b = reader.ReadByte();
                    value = ((b == 0) ? null : reader.ReadString());
                    return(true);
                }

                case DObject.TypeCode.Guid:
                    value = new Guid(reader.ReadBytes(16));
                    return(true);

                default:
                    throw new ArgumentOutOfRangeException("type", type, null);
                }
            }
            int num = reader.ReadInt32();

            if (num < 0)
            {
                value = null;
                scope = null;
                return(true);
            }
            if (num < 1)
            {
                scope = null;
                if (type == DObject.TypeCode.DObject)
                {
                    value = new DObject();
                    return(true);
                }
                switch (type & ~DObject.TypeCode.Array)
                {
                case DObject.TypeCode.DObject:
                    value = ZeroLengthArrays.DObject;
                    return(true);

                case DObject.TypeCode.Boolean:
                    value = ZeroLengthArrays.Boolean;
                    return(true);

                case DObject.TypeCode.Char:
                    value = ZeroLengthArrays.Char;
                    return(true);

                case DObject.TypeCode.SByte:
                    value = ZeroLengthArrays.SByte;
                    return(true);

                case DObject.TypeCode.Byte:
                    value = ZeroLengthArrays.Byte;
                    return(true);

                case DObject.TypeCode.Int16:
                    value = ZeroLengthArrays.Int16;
                    return(true);

                case DObject.TypeCode.UInt16:
                    value = ZeroLengthArrays.UInt16;
                    return(true);

                case DObject.TypeCode.Int32:
                    value = ZeroLengthArrays.Int32;
                    return(true);

                case DObject.TypeCode.UInt32:
                    value = ZeroLengthArrays.UInt32;
                    return(true);

                case DObject.TypeCode.Int64:
                    value = ZeroLengthArrays.Int64;
                    return(true);

                case DObject.TypeCode.UInt64:
                    value = ZeroLengthArrays.UInt64;
                    return(true);

                case DObject.TypeCode.Single:
                    value = ZeroLengthArrays.Single;
                    return(true);

                case DObject.TypeCode.Double:
                    value = ZeroLengthArrays.Double;
                    return(true);

                case DObject.TypeCode.Decimal:
                    value = ZeroLengthArrays.Decimal;
                    return(true);

                case DObject.TypeCode.DateTime:
                    value = ZeroLengthArrays.DateTime;
                    return(true);

                case DObject.TypeCode.TimeSpan:
                    value = ZeroLengthArrays.TimeSpan;
                    return(true);

                case DObject.TypeCode.String:
                    value = ZeroLengthArrays.String;
                    return(true);

                case DObject.TypeCode.Guid:
                    value = ZeroLengthArrays.Guid;
                    return(true);

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            if (type == DObject.TypeCode.DObject)
            {
                scope = new DeserializationScope(new DObject(), num);
                value = null;
                return(false);
            }
            scope = new DeserializationScope(new List <object>(), type, num);
            value = null;
            return(false);
        }
コード例 #2
0
        public static void Read(BinaryReader reader, out DObject.TypeCode type, out object value)
        {
            type = (DObject.TypeCode)reader.ReadInt32();
            if (ReadIfPrimitiveOrNull(reader, type, out value, out var scope))
            {
                return;
            }
            Stack <DeserializationScope> stack = new Stack <DeserializationScope>();

            while (true)
            {
                int    num;
                string text;
                DeserializationScope scope2;
                int num2;
                if (scope.Type == DObject.TypeCode.DObject)
                {
                    DObject obj   = (DObject)scope.Instance;
                    int     count = scope.Count;
                    num = scope.Index;
                    while (num < count)
                    {
                        text = reader.ReadString();
                        DObject.TypeCode type2 = (DObject.TypeCode)reader.ReadInt32();
                        if (ReadIfPrimitiveOrNull(reader, type2, out var value2, out scope2))
                        {
                            AddToDObject(obj, text, type2, value2);
                            num++;
                            continue;
                        }
                        goto IL_0071;
                    }
                }
                else
                {
                    List <object>    list     = (List <object>)scope.Instance;
                    int              count2   = scope.Count;
                    DObject.TypeCode typeCode = scope.Type & ~DObject.TypeCode.Array;
                    num2 = scope.Index;
                    while (num2 < count2)
                    {
                        if (ReadIfPrimitiveOrNull(reader, typeCode, out var value3, out scope2))
                        {
                            list.Add(value3);
                            num2++;
                            continue;
                        }
                        goto IL_00df;
                    }
                    scope.Instance = ListToTypedArray(list, typeCode);
                }
                if (stack.Count < 1)
                {
                    break;
                }
                DeserializationScope deserializationScope = scope;
                scope = stack.Pop();
                if (scope.Type == DObject.TypeCode.DObject)
                {
                    AddToDObject((DObject)scope.Instance, scope.LastKey, deserializationScope.Type, deserializationScope.Instance);
                }
                else
                {
                    ((List <object>)scope.Instance).Add(deserializationScope.Instance);
                }
                continue;
IL_0071:
                scope.Index   = num + 1;
                scope.LastKey = text;
                stack.Push(scope);
                scope = scope2;
                continue;
IL_00df:
                scope.Index = num2 + 1;
                stack.Push(scope);
                scope = scope2;
            }
            value = scope.Instance;
        }