Пример #1
0
 public NetObjectSerializer(TypeModel model, Type type, int key, BclHelpers.NetObjectOptions options)
 {
     bool flag = (byte)(options & BclHelpers.NetObjectOptions.DynamicType) != 0;
     this.key = ((!flag) ? key : -1);
     this.type = ((!flag) ? type : model.MapType(typeof(object)));
     this.options = options;
 }
Пример #2
0
 public NetObjectSerializer(TypeModel model, Type type, int key, BclHelpers.NetObjectOptions options)
 {
     bool dynamicType = (options & BclHelpers.NetObjectOptions.DynamicType) != 0;
     this.key = dynamicType ? -1 : key;
     this.type = dynamicType ? model.MapType(typeof (object)) : type;
     this.options = options;
 }
        public void ShouldSerializeDecimalValue()
        {
            // Arrange
            var value      = 42m;
            var dataReader = this.CreateDataReader(value);

            // Act
            this.writer.Serialize(this.stream, dataReader);

            // Assert
            this.stream.Position = 0;

            this.ReadUntilFieldValue();

            Assert.Equal(value, BclHelpers.ReadDecimal(this.reader));
        }
        public void ShouldSerializeDateTimeValue()
        {
            // Arrange
            var value      = new DateTime(1969, 10, 29, 22, 30, 0);
            var dataReader = this.CreateDataReader(value);

            // Act
            this.writer.Serialize(this.stream, dataReader);

            // Assert
            this.stream.Position = 0;

            this.ReadUntilFieldValue();

            Assert.Equal(value, BclHelpers.ReadDateTime(this.reader));
        }
        public static void WriteValue(int fieldIndex, object value, ProtoWriter.State protoWriter)
        {
            if (value == null)
            {
                return;
            }
            switch (value)
            {
            case bool boolean:
                protoWriter.WriteFieldHeader(fieldIndex, WireType.Varint);
                protoWriter.WriteBoolean(boolean);
                break;

            case DateTime dateTime:
                protoWriter.WriteFieldHeader(fieldIndex, WireType.StartGroup);
                BclHelpers.WriteDateTime(ref protoWriter, dateTime);
                break;

            case int intValue:
                protoWriter.WriteFieldHeader(fieldIndex, WireType.Varint);
                protoWriter.WriteInt32(intValue);
                break;

            case short shortValue:
                protoWriter.WriteFieldHeader(fieldIndex, WireType.Varint);
                protoWriter.WriteInt16(shortValue);
                break;

            case double doubleValue:
                protoWriter.WriteFieldHeader(fieldIndex, WireType.Fixed64);
                protoWriter.WriteDouble(doubleValue);
                break;

            case string stringValue:
                protoWriter.WriteFieldHeader(fieldIndex, WireType.String);
                protoWriter.WriteString(stringValue);
                break;

            case Guid guidValue:
                protoWriter.WriteFieldHeader(fieldIndex, WireType.StartGroup);
                BclHelpers.WriteGuid(ref protoWriter, guidValue);
                break;

            default:
                throw new ProtoException("Unrecognized data type");
            }
        }
Пример #6
0
        public object Read(object value, ProtoReader source)
        {
            Type        type;
            object      obj         = value;
            ProtoReader protoReader = source;
            int         num         = this.key;

            if (this.type == typeof(object))
            {
                type = null;
            }
            else
            {
                type = this.type;
            }
            return(BclHelpers.ReadNetObject(obj, protoReader, num, type, this.options));
        }
Пример #7
0
        object CreateInstance(ProtoReader source, bool includeLocalCallback)
        {
            //Helpers.DebugWriteLine("* creating : " + forType.FullName);
            object obj;

            if (factory != null)
            {
                obj = InvokeCallback(factory, null, source.Context);
            }
            else if (useConstructor)
            {
                if (!hasConstructor)
                {
                    TypeModel.ThrowCannotCreateInstance(constructType);
                }
#if PROFILE259
                ConstructorInfo constructorInfo = System.Linq.Enumerable.First(
                    constructType.GetTypeInfo().DeclaredConstructors, c => c.GetParameters().Length == 0);
                obj = constructorInfo.Invoke(new object[] {});
#else
                obj = Activator.CreateInstance(constructType
#if !(CF || SILVERLIGHT || WINRT || PORTABLE || NETSTANDARD1_3 || NETSTANDARD1_4)
                                               , nonPublic: true
#endif
                                               );
#endif
            }
            else
            {
                obj = BclHelpers.GetUninitializedObject(constructType);
            }
            ProtoReader.NoteObject(obj, source);
            if (baseCtorCallbacks != null)
            {
                for (int i = 0; i < baseCtorCallbacks.Length; i++)
                {
                    InvokeCallback(baseCtorCallbacks[i], obj, source.Context);
                }
            }
            if (includeLocalCallback && callbacks != null)
            {
                InvokeCallback(callbacks.BeforeDeserialize, obj, source.Context);
            }
            return(obj);
        }
        object CreateInstance(ProtoReader source, bool includeLocalCallback)
        {
            //Helpers.DebugWriteLine("* creating : " + forType.FullName);
            object obj;

            if (factory != null)
            {
                obj = InvokeCallback(factory, null, source.Context);
            }
            else if (useConstructor)
            {
                if (!hasConstructor)
                {
                    TypeModel.ThrowCannotCreateInstance(constructType);
                }
                // ## 苦竹 修改 ##
                //        obj = Activator.CreateInstance(constructType
                //#if !(CF || SILVERLIGHT || WINRT || PORTABLE || NETSTANDARD1_3 || NETSTANDARD1_4)
                //                    , nonPublic: true
                //#endif
                //                    );
                obj = ActivatorUtils.FastCreateInstance(constructType);
            }
            else
            {
                obj = BclHelpers.GetUninitializedObject(constructType);
            }
            ProtoReader.NoteObject(obj, source);
            if (baseCtorCallbacks != null)
            {
                for (int i = 0; i < baseCtorCallbacks.Length; i++)
                {
                    InvokeCallback(baseCtorCallbacks[i], obj, source.Context);
                }
            }
            if (includeLocalCallback && callbacks != null)
            {
                InvokeCallback(callbacks.BeforeDeserialize, obj, source.Context);
            }
            return(obj);
        }
Пример #9
0
#pragma warning disable xUnit1026
        public void CheckDecimalLayout(bool optimized)
#pragma warning restore xUnit1026
        {
            bool    oldVal = BclHelpers.GuidOptimized;
            decimal value  = 1.0000000000045000000003000000M;

            try
            {
#if DEBUG
                BclHelpers.DecimalOptimized = optimized;
#endif
                using (var ms = new MemoryStream())
                {
                    using (var writer = ProtoWriter.Create(out var state, ms, RuntimeTypeModel.Default))
                    {
                        ProtoWriter.WriteFieldHeader(1, WireType.String, writer, ref state);
                        BclHelpers.WriteDecimal(value, writer, ref state);
                        writer.Close(ref state);
                    }
                    var hex = BitConverter.ToString(
                        ms.GetBuffer(), 0, (int)ms.Length);
                    Assert.Equal("0A-12-08-C0-8D-C9-B8-C0-B4-B8-E2-3E-10-DE-9C-BF-82-02-18-38", hex);

                    ms.Position = 0;
                    using (var reader = ProtoReader.Create(out var state, ms, RuntimeTypeModel.Default))
                    {
                        Assert.Equal(1, reader.ReadFieldHeader(ref state));
                        var result = BclHelpers.ReadDecimal(reader, ref state);
                        Assert.Equal(value, result);
                        Assert.Equal(0, reader.ReadFieldHeader(ref state));
                    }
                }
            }
            finally
            {
#if DEBUG
                BclHelpers.DecimalOptimized = oldVal;
#endif
            }
        }
Пример #10
0
        object CreateInstance(ProtoReader source, bool includeLocalCallback)
        {
            //Helpers.DebugWriteLine("* creating : " + forType.FullName);
            object obj;

            if (factory != null)
            {
                obj = InvokeCallback(factory, null, source.Context);
            }
            else if (useConstructor)
            {
                if (!hasConstructor)
                {
                    TypeModel.ThrowCannotCreateInstance(constructType);
                }

                obj = Activator.CreateInstance(constructType
#if !CF && !SILVERLIGHT && !WINRT && !PORTABLE && !COREFX
                                               , true
#endif
                                               );
            }
            else
            {
                obj = BclHelpers.GetUninitializedObject(constructType);
            }
            ProtoReader.NoteObject(obj, source);
            if (baseCtorCallbacks != null)
            {
                for (int i = 0; i < baseCtorCallbacks.Length; i++)
                {
                    InvokeCallback(baseCtorCallbacks[i], obj, source.Context);
                }
            }
            if (includeLocalCallback && callbacks != null)
            {
                InvokeCallback(callbacks.BeforeDeserialize, obj, source.Context);
            }
            return(obj);
        }
Пример #11
0
        private void CheckGuidLayoutImpl(MemoryStream ms, Guid value, string expected)
        {
            ms.Position = 0;
            ms.SetLength(0);
            var state = ProtoWriter.State.Create(ms, RuntimeTypeModel.Default);

            try
            {
                state.WriteFieldHeader(1, WireType.String);
                BclHelpers.WriteGuid(ref state, value);
                state.Close();
            }
            finally
            {
                state.Dispose();
            }

            if (expected != null)
            {
                var hex = BitConverter.ToString(
                    ms.GetBuffer(), 0, (int)ms.Length);
                Assert.Equal(expected, hex);
            }

            ms.Position = 0;
            var readState = ProtoReader.State.Create(ms, RuntimeTypeModel.Default);

            try
            {
                Assert.Equal(1, readState.ReadFieldHeader());
                var result = BclHelpers.ReadGuid(ref readState);
                Assert.Equal(value, result);
                Assert.Equal(0, readState.ReadFieldHeader());
            }
            finally
            {
                readState.Dispose();
            }
        }
Пример #12
0
        object CreateInstance(ProtoReader source, bool includeLocalCallback)
        {
            //Helpers.DebugWriteLine("* creating : " + forType.FullName);
            object obj;

            if (factory != null)
            {
                obj = InvokeCallback(factory, null, source.Context);
            }
            else if (useConstructor)
            {
                if (!hasConstructor)
                {
                    TypeModel.ThrowCannotCreateInstance(constructType);
                }
                obj = PType.CreateInstance(constructType);
            }
            else
            {
                obj = BclHelpers.GetUninitializedObject(constructType);
            }
            ProtoReader.NoteObject(obj, source);
            if (baseCtorCallbacks != null)
            {
                for (int i = 0; i < baseCtorCallbacks.Length; i++)
                {
                    InvokeCallback(baseCtorCallbacks[i], obj, source.Context);
                }
            }
            if (includeLocalCallback && callbacks != null)
            {
                InvokeCallback(callbacks.BeforeDeserialize, obj, source.Context);
            }
            if (obj == null)
            {
                throw new ProtoException("obj is null.");
            }
            return(obj);
        }
Пример #13
0
 public void Write(object value, ProtoWriter dest)
 {
     BclHelpers.WriteTimeSpan((TimeSpan)value, dest);
 }
Пример #14
0
 public void Write(object value, ProtoWriter dest)
 {
     BclHelpers.WriteDecimal((decimal)value, dest);
 }
Пример #15
0
 public object Read(ref ProtoReader.State state, object value)
 {
     Debug.Assert(value == null); // since replaces
     return(BclHelpers.ReadGuid(ref state));
 }
Пример #16
0
 public void Write(ref ProtoWriter.State state, object value)
 {
     BclHelpers.WriteGuid(ref state, (Guid)value);
 }
Пример #17
0
        static DataTable ProtoRead(Stream stream)
        {
            DataTable table = new DataTable();

            object[] values = null;
            using (ProtoReader reader = new ProtoReader(stream, null, null))
            {
                int field;
                List <Func <object> > colReaders = new List <Func <object> >();
                SubItemToken          token;
                while ((field = reader.ReadFieldHeader()) != 0)
                {
                    switch (field)
                    {
                    case 1:
                        table.TableName = reader.ReadString();
                        break;

                    case 2:
                        string     name       = null;
                        MappedType mappedType = (MappedType)(-1);
                        token = ProtoReader.StartSubItem(reader);
                        while ((field = reader.ReadFieldHeader()) != 0)
                        {
                            switch (field)
                            {
                            case 1:
                                name = reader.ReadString();
                                break;

                            case 2:
                                mappedType = (MappedType)reader.ReadInt32();
                                break;

                            default:
                                reader.SkipField();
                                break;
                            }
                        }
                        Type type;
                        switch (mappedType)
                        {
                        case MappedType.Int32:
                            type = typeof(int);
                            colReaders.Add(() => reader.ReadInt32());
                            break;

                        case MappedType.Int16:
                            type = typeof(short);
                            colReaders.Add(() => reader.ReadInt16());
                            break;

                        case MappedType.Decimal:
                            type = typeof(decimal);
                            colReaders.Add(() => BclHelpers.ReadDecimal(reader));
                            break;

                        case MappedType.String:
                            type = typeof(string);
                            colReaders.Add(() => reader.ReadString());
                            break;

                        case MappedType.Guid:
                            type = typeof(Guid);
                            colReaders.Add(() => BclHelpers.ReadGuid(reader));
                            break;

                        case MappedType.DateTime:
                            type = typeof(DateTime);
                            colReaders.Add(() => BclHelpers.ReadDateTime(reader));
                            break;

                        default:
                            throw new NotSupportedException(mappedType.ToString());
                        }
                        ProtoReader.EndSubItem(token, reader);
                        table.Columns.Add(name, type);
                        values = null;
                        break;

                    case 3:
                        if (values == null)
                        {
                            values = new object[table.Columns.Count];
                        }
                        else
                        {
                            Array.Clear(values, 0, values.Length);
                        }
                        token = ProtoReader.StartSubItem(reader);
                        while ((field = reader.ReadFieldHeader()) != 0)
                        {
                            if (field > values.Length)
                            {
                                reader.SkipField();
                            }
                            else
                            {
                                int i = field - 1;
                                values[i] = colReaders[i]();
                            }
                        }
                        ProtoReader.EndSubItem(token, reader);
                        table.Rows.Add(values);
                        break;

                    default:
                        reader.SkipField();
                        break;
                    }
                }
            }
            return(table);
        }
Пример #18
0
        static void ProtoWrite(DataTable table, Stream stream)
        {
            using (var writer = new ProtoWriter(stream, null, null))
            {
                // table name
                if (!string.IsNullOrEmpty(table.TableName))
                {
                    ProtoWriter.WriteFieldHeader(1, WireType.String, writer);
                    ProtoWriter.WriteString(table.TableName, writer);
                }

                // write the schema:
                var cols = table.Columns;
                Action <object>[] colWriters = new Action <object> [cols.Count];
                int i = 0;

                foreach (DataColumn col in cols)
                {
                    // for each, write the name and data type
                    ProtoWriter.WriteFieldHeader(2, WireType.StartGroup, writer);
                    var token = ProtoWriter.StartSubItem(col, writer);
                    ProtoWriter.WriteFieldHeader(1, WireType.String, writer);
                    ProtoWriter.WriteString(col.ColumnName, writer);
                    ProtoWriter.WriteFieldHeader(2, WireType.Variant, writer);
                    MappedType type;
                    switch (Type.GetTypeCode(col.DataType))
                    {
                    case TypeCode.Decimal: type = MappedType.Decimal; break;

                    case TypeCode.Int16: type = MappedType.Int16; break;

                    case TypeCode.Int32: type = MappedType.Int32; break;

                    case TypeCode.String: type = MappedType.String; break;

                    case TypeCode.DateTime: type = MappedType.DateTime; break;

                    default:
                        if (col.DataType == typeof(Guid))
                        {
                            type = MappedType.Guid; break;
                        }
                        throw new NotSupportedException(col.DataType.Name);
                    }
                    ProtoWriter.WriteInt32((int)type, writer);
                    ProtoWriter.EndSubItem(token, writer);
                    int             field = i + 1;
                    Action <object> colWriter;
                    switch (type)
                    {
                    case MappedType.String:
                        colWriter = value =>
                        {
                            ProtoWriter.WriteFieldHeader(field, WireType.String, writer);
                            ProtoWriter.WriteString((string)value, writer);
                        };
                        break;

                    case MappedType.Int16:
                        colWriter = value =>
                        {
                            ProtoWriter.WriteFieldHeader(field, WireType.Variant, writer);
                            ProtoWriter.WriteInt16((short)value, writer);
                        };
                        break;

                    case MappedType.Decimal:
                        colWriter = value =>
                        {
                            ProtoWriter.WriteFieldHeader(field, WireType.StartGroup, writer);
                            BclHelpers.WriteDecimal((decimal)value, writer);
                        };
                        break;

                    case MappedType.Int32:
                        colWriter = value =>
                        {
                            ProtoWriter.WriteFieldHeader(field, WireType.Variant, writer);
                            ProtoWriter.WriteInt32((int)value, writer);
                        };
                        break;

                    case MappedType.Guid:
                        colWriter = value =>
                        {
                            ProtoWriter.WriteFieldHeader(field, WireType.StartGroup, writer);
                            BclHelpers.WriteGuid((Guid)value, writer);
                        };
                        break;

                    case MappedType.DateTime:
                        colWriter = value =>
                        {
                            ProtoWriter.WriteFieldHeader(field, WireType.StartGroup, writer);
                            BclHelpers.WriteDateTime((DateTime)value, writer);
                        };
                        break;

                    default:
                        throw new NotSupportedException(col.DataType.Name);
                    }
                    colWriters[i++] = colWriter;
                }
                // write the rows
                foreach (DataRow row in table.Rows)
                {
                    i = 0;
                    ProtoWriter.WriteFieldHeader(3, WireType.StartGroup, writer);
                    var token = ProtoWriter.StartSubItem(row, writer);
                    foreach (DataColumn col in cols)
                    {
                        var value = row[col];
                        if (value == null || value is DBNull)
                        {
                        }
                        else
                        {
                            colWriters[i](value);
                        }
                        i++;
                    }
                    ProtoWriter.EndSubItem(token, writer);
                }
            }
        }
Пример #19
0
        private static void Merge(ProtoReader reader, ref ProtoReader.State state, ref protogen.Order obj)
        {
            SubItemToken tok;
            int          field;

            if (obj == null)
            {
                obj = new protogen.Order();
            }
            while ((field = reader.ReadFieldHeader(ref state)) != 0)
            {
                switch (field)
                {
                case 1:
                    obj.OrderID = reader.ReadInt32(ref state);
                    break;

                case 2:
                    obj.CustomerID = reader.ReadString(ref state);
                    break;

                case 3:
                    obj.EmployeeID = reader.ReadInt32(ref state);
                    break;

                case 4:
                    obj.OrderDate = BclHelpers.ReadTimestamp(reader, ref state);
                    break;

                case 5:
                    obj.RequiredDate = BclHelpers.ReadTimestamp(reader, ref state);
                    break;

                case 6:
                    obj.ShippedDate = BclHelpers.ReadTimestamp(reader, ref state);
                    break;

                case 7:
                    obj.ShipVia = reader.ReadInt32(ref state);
                    break;

                case 8:
                    obj.Freight = reader.ReadDouble(ref state);
                    break;

                case 9:
                    obj.ShipName = reader.ReadString(ref state);
                    break;

                case 10:
                    obj.ShipAddress = reader.ReadString(ref state);
                    break;

                case 11:
                    obj.ShipCity = reader.ReadString(ref state);
                    break;

                case 12:
                    obj.ShipRegion = reader.ReadString(ref state);
                    break;

                case 13:
                    obj.ShipPostalCode = reader.ReadString(ref state);
                    break;

                case 14:
                    obj.ShipCountry = reader.ReadString(ref state);
                    break;

                case 15:
                    do
                    {
                        protogen.OrderLine _15 = default;
                        tok = ProtoReader.StartSubItem(reader, ref state);
                        Merge(reader, ref state, ref _15);
                        ProtoReader.EndSubItem(tok, reader, ref state);
                        obj.Lines.Add(_15);
                    } while (reader.TryReadFieldHeader(ref state, 1));
                    break;

                default:
                    reader.AppendExtensionData(ref state, obj);
                    break;
                }
            }
        }
Пример #20
0
 // Token: 0x060001B8 RID: 440 RVA: 0x00003276 File Offset: 0x00001476
 public void Write(object value, ProtoWriter dest)
 {
     BclHelpers.WriteDateTime((DateTime)value, dest);
 }
Пример #21
0
 // Token: 0x060001B7 RID: 439 RVA: 0x00003269 File Offset: 0x00001469
 public object Read(object value, ProtoReader source)
 {
     return(BclHelpers.ReadDateTime(source));
 }
Пример #22
0
 public void Write(object value, ProtoWriter dest)
 {
     BclHelpers.WriteGuid((Guid)value, dest);
 }
 public object Read(object value, ProtoReader source)
 {
     return(BclHelpers.ReadNetObject(value, source, key, type == typeof(object) ? null : type, options));
 }
 public object Read(object value, ProtoReader source)
 {
     return(BclHelpers.ReadGuid(source));
 }
Пример #25
0
        public static DataTable ProtoRead(Stream stream)
        {
            DataTable table = new DataTable();

            object[]      array  = null;
            Func <object> item   = null;
            Func <object> func2  = null;
            Func <object> func3  = null;
            Func <object> func4  = null;
            Func <object> func5  = null;
            Func <object> func6  = null;
            Func <object> func7  = null;
            Func <object> func8  = null;
            Func <object> func9  = null;
            Func <object> func10 = null;
            Func <object> func11 = null;

            using (ProtoReader reader = new ProtoReader(stream, null, null))
            {
                int num;
                List <Func <object> > list = new List <Func <object> >();
                while ((num = reader.ReadFieldHeader()) != 0)
                {
                    SubItemToken token;
                    string       str;
                    string       str2;
                    MappedType   type;
                    Type         type2;
                    switch (num)
                    {
                    case 1:
                    {
                        table.TableName = reader.ReadString();
                        continue;
                    }

                    case 2:
                        str   = null;
                        str2  = string.Empty;
                        type  = ~MappedType.Boolean;
                        token = ProtoReader.StartSubItem(reader);
                        goto Label_00F1;

                    case 3:
                        if (array != null)
                        {
                            goto Label_0382;
                        }
                        array = new object[table.Columns.Count];
                        goto Label_038C;

                    default:
                        goto Label_03F3;
                    }
Label_009B:
                    switch (num)
                    {
                    case 1:
                        str = reader.ReadString();
                        break;

                    case 2:
                        type = (MappedType)reader.ReadInt32();
                        break;

                    case 3:
                        str2 = reader.ReadString();
                        break;

                    default:
                        reader.SkipField();
                        break;
                    }
Label_00F1:
                    if ((num = reader.ReadFieldHeader()) != 0)
                    {
                        goto Label_009B;
                    }
                    switch (type)
                    {
                    case MappedType.Boolean:
                        type2 = typeof(bool);
                        if (item == null)
                        {
                            item = () => reader.ReadBoolean();
                        }
                        list.Add(item);
                        break;

                    case MappedType.Byte:
                        type2 = typeof(byte[]);
                        if (func2 == null)
                        {
                            func2 = () => ReadBytes(reader);
                        }
                        list.Add(func2);
                        break;

                    case MappedType.Double:
                        type2 = typeof(double);
                        if (func3 == null)
                        {
                            func3 = () => reader.ReadDouble();
                        }
                        list.Add(func3);
                        break;

                    case MappedType.Int16:
                        type2 = typeof(short);
                        if (func6 == null)
                        {
                            func6 = () => reader.ReadInt16();
                        }
                        list.Add(func6);
                        break;

                    case MappedType.Int32:
                        type2 = typeof(int);
                        if (func5 == null)
                        {
                            func5 = () => reader.ReadInt32();
                        }
                        list.Add(func5);
                        break;

                    case MappedType.Int64:
                        type2 = typeof(long);
                        if (func4 == null)
                        {
                            func4 = () => reader.ReadInt64();
                        }
                        list.Add(func4);
                        break;

                    case MappedType.String:
                        type2 = typeof(string);
                        if (func8 == null)
                        {
                            func8 = () => reader.ReadString();
                        }
                        list.Add(func8);
                        break;

                    case MappedType.Decimal:
                        type2 = typeof(decimal);
                        if (func7 == null)
                        {
                            func7 = () => BclHelpers.ReadDecimal(reader);
                        }
                        list.Add(func7);
                        break;

                    case MappedType.Guid:
                        type2 = typeof(Guid);
                        if (func9 == null)
                        {
                            func9 = () => BclHelpers.ReadGuid(reader);
                        }
                        list.Add(func9);
                        break;

                    case MappedType.DateTime:
                        type2 = typeof(DateTime);
                        if (func10 == null)
                        {
                            func10 = () => BclHelpers.ReadDateTime(reader);
                        }
                        list.Add(func10);
                        break;

                    case MappedType.TimeSpan:
                        type2 = typeof(TimeSpan);
                        if (func11 == null)
                        {
                            func11 = () => BclHelpers.ReadTimeSpan(reader);
                        }
                        list.Add(func11);
                        break;

                    default:
                        throw new NotSupportedException(type.ToString());
                    }
                    ProtoReader.EndSubItem(token, reader);
                    table.Columns.Add(str, type2);
                    if (!string.IsNullOrEmpty(str2))
                    {
                        table.Columns[str].Caption = str2;
                    }
                    array = null;
                    continue;
Label_0382:
                    Array.Clear(array, 0, array.Length);
Label_038C:
                    token = ProtoReader.StartSubItem(reader);
                    while ((num = reader.ReadFieldHeader()) != 0)
                    {
                        if (num > array.Length)
                        {
                            reader.SkipField();
                        }
                        else
                        {
                            int index = num - 1;
                            array[index] = list[index]();
                        }
                    }
                    ProtoReader.EndSubItem(token, reader);
                    table.Rows.Add(array);
                    continue;
Label_03F3:
                    reader.SkipField();
                }
            }
            return(table);
        }
Пример #26
0
 public void Write(object value, ProtoWriter dest)
 {
     BclHelpers.WriteGuid(ValueObject.Value <Guid>(value), dest);
 }
Пример #27
0
 // Token: 0x060035A6 RID: 13734 RVA: 0x00134C0A File Offset: 0x0013300A
 public object Read(object value, ProtoReader source)
 {
     return(BclHelpers.ReadNetObject(value, source, key, (type != typeof(object)) ? type : null, options));
 }
Пример #28
0
        public static void ProtoWrite(DataTable table, Stream stream)
        {
            using (ProtoWriter writer = new ProtoWriter(stream, null, null))
            {
                if (!string.IsNullOrEmpty(table.TableName))
                {
                    ProtoWriter.WriteFieldHeader(1, WireType.String, writer);
                    ProtoWriter.WriteString(table.TableName, writer);
                }
                DataColumnCollection columns     = table.Columns;
                Action <object>[]    actionArray = new Action <object> [columns.Count];
                int index = 0;
                foreach (DataColumn column in columns)
                {
                    MappedType      boolean;
                    Action <object> action;
                    Action <object> action2  = null;
                    Action <object> action3  = null;
                    Action <object> action4  = null;
                    Action <object> action5  = null;
                    Action <object> action6  = null;
                    Action <object> action7  = null;
                    Action <object> action8  = null;
                    Action <object> action9  = null;
                    Action <object> action10 = null;
                    Action <object> action11 = null;
                    Action <object> action12 = null;
                    ProtoWriter.WriteFieldHeader(2, WireType.StartGroup, writer);
                    SubItemToken token = ProtoWriter.StartSubItem(column, writer);
                    ProtoWriter.WriteFieldHeader(1, WireType.String, writer);
                    ProtoWriter.WriteString(column.ColumnName, writer);
                    ProtoWriter.WriteFieldHeader(2, WireType.Variant, writer);
                    switch (Type.GetTypeCode(column.DataType))
                    {
                    case TypeCode.Boolean:
                        boolean = MappedType.Boolean;
                        break;

                    case TypeCode.Byte:
                        boolean = MappedType.Byte;
                        break;

                    case TypeCode.Int16:
                        boolean = MappedType.Int16;
                        break;

                    case TypeCode.Int32:
                        boolean = MappedType.Int32;
                        break;

                    case TypeCode.Int64:
                        boolean = MappedType.Int64;
                        break;

                    case TypeCode.Double:
                        boolean = MappedType.Double;
                        break;

                    case TypeCode.Decimal:
                        boolean = MappedType.Decimal;
                        break;

                    case TypeCode.DateTime:
                        boolean = MappedType.DateTime;
                        break;

                    case TypeCode.String:
                        boolean = MappedType.String;
                        break;

                    default:
                        if (column.DataType == typeof(Guid))
                        {
                            boolean = MappedType.Guid;
                        }
                        else if (column.DataType == typeof(byte[]))
                        {
                            boolean = MappedType.Byte;
                        }
                        else
                        {
                            if (column.DataType != typeof(TimeSpan))
                            {
                                throw new NotSupportedException(column.DataType.Name);
                            }
                            boolean = MappedType.TimeSpan;
                        }
                        break;
                    }
                    ProtoWriter.WriteInt32((int)boolean, writer);
                    ProtoWriter.WriteFieldHeader(3, WireType.String, writer);
                    ProtoWriter.WriteString(column.Caption, writer);
                    ProtoWriter.EndSubItem(token, writer);
                    int field = index + 1;
                    switch (boolean)
                    {
                    case MappedType.Boolean:
                        if (action2 == null)
                        {
                            action2 = delegate(object value)
                            {
                                ProtoWriter.WriteFieldHeader(field, WireType.Variant, writer);
                                ProtoWriter.WriteBoolean(Convert.ToBoolean(value), writer);
                            };
                        }
                        action = action2;
                        break;

                    case MappedType.Byte:
                        if (action3 == null)
                        {
                            action3 = delegate(object value)
                            {
                                byte[] inArray = value as byte[];
                                string str     = Convert.ToBase64String(inArray);
                                ProtoWriter.WriteFieldHeader(field, WireType.String, writer);
                                ProtoWriter.WriteString(str, writer);
                            };
                        }
                        action = action3;
                        break;

                    case MappedType.Double:
                        if (action4 == null)
                        {
                            action4 = delegate(object value)
                            {
                                ProtoWriter.WriteFieldHeader(field, WireType.Fixed64, writer);
                                ProtoWriter.WriteDouble(Convert.ToDouble(value), writer);
                            };
                        }
                        action = action4;
                        break;

                    case MappedType.Int16:
                        if (action7 == null)
                        {
                            action7 = delegate(object value)
                            {
                                ProtoWriter.WriteFieldHeader(field, WireType.Variant, writer);
                                ProtoWriter.WriteInt16((short)value, writer);
                            };
                        }
                        action = action7;
                        break;

                    case MappedType.Int32:
                        if (action8 == null)
                        {
                            action8 = delegate(object value)
                            {
                                ProtoWriter.WriteFieldHeader(field, WireType.Variant, writer);
                                ProtoWriter.WriteInt32(Convert.ToInt32(value), writer);
                            };
                        }
                        action = action8;
                        break;

                    case MappedType.Int64:
                        if (action9 == null)
                        {
                            action9 = delegate(object value)
                            {
                                ProtoWriter.WriteFieldHeader(field, WireType.Variant, writer);
                                ProtoWriter.WriteInt64((long)value, writer);
                            };
                        }
                        action = action9;
                        break;

                    case MappedType.String:
                        if (action5 == null)
                        {
                            action5 = delegate(object value)
                            {
                                ProtoWriter.WriteFieldHeader(field, WireType.String, writer);
                                ProtoWriter.WriteString(Convert.ToString(value), writer);
                            };
                        }
                        action = action5;
                        break;

                    case MappedType.Decimal:
                        if (action6 == null)
                        {
                            action6 = delegate(object value)
                            {
                                ProtoWriter.WriteFieldHeader(field, WireType.StartGroup, writer);
                                BclHelpers.WriteDecimal(Convert.ToDecimal(value), writer);
                            };
                        }
                        action = action6;
                        break;

                    case MappedType.Guid:
                        if (action10 == null)
                        {
                            action10 = delegate(object value)
                            {
                                ProtoWriter.WriteFieldHeader(field, WireType.StartGroup, writer);
                                BclHelpers.WriteGuid((Guid)value, writer);
                            };
                        }
                        action = action10;
                        break;

                    case MappedType.DateTime:
                        if (action11 == null)
                        {
                            action11 = delegate(object value)
                            {
                                ProtoWriter.WriteFieldHeader(field, WireType.StartGroup, writer);
                                BclHelpers.WriteDateTime(Convert.ToDateTime(value), writer);
                            };
                        }
                        action = action11;
                        break;

                    case MappedType.TimeSpan:
                        if (action12 == null)
                        {
                            action12 = delegate(object value)
                            {
                                ProtoWriter.WriteFieldHeader(field, WireType.StartGroup, writer);
                                if (value is TimeSpan)
                                {
                                    BclHelpers.WriteTimeSpan((TimeSpan)value, writer);
                                }
                                else
                                {
                                    BclHelpers.WriteTimeSpan(TimeSpan.Zero, writer);
                                }
                            };
                        }
                        action = action12;
                        break;

                    default:
                        throw new NotSupportedException(column.DataType.Name);
                    }
                    actionArray[index++] = action;
                }
                foreach (DataRow row in table.Rows)
                {
                    index = 0;
                    ProtoWriter.WriteFieldHeader(3, WireType.StartGroup, writer);
                    SubItemToken token2 = ProtoWriter.StartSubItem(row, writer);
                    foreach (DataColumn column2 in columns)
                    {
                        object obj2 = row[column2];
                        if ((obj2 != null) && !(obj2 is DBNull))
                        {
                            actionArray[index](obj2);
                        }
                        index++;
                    }
                    ProtoWriter.EndSubItem(token2, writer);
                }
            }
        }
 public void Write(object value, ProtoWriter dest)
 {
     BclHelpers.WriteNetObject(value, dest, key, options);
 }
Пример #30
0
        public static void WriteRecord(ProtoWriterContext context, int recordIndex, IDataRecord record)
        {
            ProtoWriter.WriteFieldHeader(RecordFieldHeader, WireType.StartGroup, context.Writer);

            context.StartSubItem(recordIndex);

            for (var columnIndex = 0; columnIndex < context.Columns.Count; columnIndex++)
            {
                // The check whether record[columnIndex] == null is removed due to the fact that value types have to be
                // boxed into an object just to check whether the field value is null. This creates garbage, thus has
                // impact on garbage collection. Removing the check should not be a problem as IDataRecord.IsDBNull
                // should be sufficient (tested with SqlDataReader, SqlLiteDataReader, and DataTableReader).
                if (record.IsDBNull(columnIndex))
                {
                    continue;
                }

                var fieldNumber = columnIndex + 1;

                switch (context.Columns[columnIndex].ProtoDataType)
                {
                case ProtoDataType.String:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.String, context.Writer);
                    ProtoWriter.WriteString(record.GetString(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Short:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteInt16(record.GetInt16(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Decimal:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.StartGroup, context.Writer);
                    BclHelpers.WriteDecimal(record.GetDecimal(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Int:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteInt32(record.GetInt32(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Guid:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.StartGroup, context.Writer);
                    BclHelpers.WriteGuid(record.GetGuid(columnIndex), context.Writer);
                    break;

                case ProtoDataType.DateTime:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.StartGroup, context.Writer);
                    BclHelpers.WriteDateTime(record.GetDateTime(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Bool:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteBoolean(record.GetBoolean(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Byte:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteByte(record.GetByte(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Char:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteInt16((short)record.GetChar(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Double:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Fixed64, context.Writer);
                    ProtoWriter.WriteDouble(record.GetDouble(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Float:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Fixed32, context.Writer);
                    ProtoWriter.WriteSingle(record.GetFloat(columnIndex), context.Writer);
                    break;

                case ProtoDataType.Long:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.Variant, context.Writer);
                    ProtoWriter.WriteInt64(record.GetInt64(columnIndex), context.Writer);
                    break;

                case ProtoDataType.ByteArray:
                    var bytes = (byte[])record[columnIndex];

                    if (bytes.Length != 0 || !context.Options.SerializeEmptyArraysAsNull)
                    {
                        ProtoWriter.WriteFieldHeader(fieldNumber, WireType.String, context.Writer);
                        ProtoWriter.WriteBytes(bytes, 0, bytes.Length, context.Writer);
                    }

                    break;

                case ProtoDataType.CharArray:
                    var characters = (char[])record[columnIndex];

                    if (characters.Length != 0 || !context.Options.SerializeEmptyArraysAsNull)
                    {
                        ProtoWriter.WriteFieldHeader(fieldNumber, WireType.String, context.Writer);
                        ProtoWriter.WriteString(new string(characters), context.Writer);
                    }

                    break;

                case ProtoDataType.TimeSpan:
                    ProtoWriter.WriteFieldHeader(fieldNumber, WireType.StartGroup, context.Writer);
                    BclHelpers.WriteTimeSpan((TimeSpan)record[columnIndex], context.Writer);
                    break;

                default:
                    throw new NotSupportedException($"The data type '{context.Columns[columnIndex].DataType.Name}' is not supported. The supported data types are: {TypeHelper.GetSupportedDataTypes()}.");
                }
            }

            context.EndSubItem();
        }
Пример #31
0
 public object Read(object value, ProtoReader source)
 {
     Helpers.DebugAssert(value == null); // since replaces
     return(BclHelpers.ReadGuid(source));
 }
Пример #32
0
        private static void ReadRecordValues(ProtoReaderContext context)
        {
            while (context.ReadFieldHeader() != NoneFieldHeader)
            {
                // Backwards compatibility or unnecessary?
                if (context.CurrentFieldHeader > context.Buffers.Length)
                {
                    context.Reader.SkipField();

                    continue;
                }

                var columnIndex = context.CurrentFieldHeader - 1;

                switch (context.Columns[columnIndex].ProtoDataType)
                {
                case ProtoDataType.String:
                    context.Buffers[columnIndex].String = context.Reader.ReadString();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.DateTime:
                    context.Buffers[columnIndex].DateTime = BclHelpers.ReadDateTime(context.Reader);
                    context.Buffers[columnIndex].IsNull   = false;
                    break;

                case ProtoDataType.Int:
                    context.Buffers[columnIndex].Int    = context.Reader.ReadInt32();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Long:
                    context.Buffers[columnIndex].Long   = context.Reader.ReadInt64();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Short:
                    context.Buffers[columnIndex].Short  = context.Reader.ReadInt16();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Bool:
                    context.Buffers[columnIndex].Bool   = context.Reader.ReadBoolean();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Byte:
                    context.Buffers[columnIndex].Byte   = context.Reader.ReadByte();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Float:
                    context.Buffers[columnIndex].Float  = context.Reader.ReadSingle();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Double:
                    context.Buffers[columnIndex].Double = context.Reader.ReadDouble();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Guid:
                    context.Buffers[columnIndex].Guid   = BclHelpers.ReadGuid(context.Reader);
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Char:
                    context.Buffers[columnIndex].Char   = (char)context.Reader.ReadInt16();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Decimal:
                    context.Buffers[columnIndex].Decimal = BclHelpers.ReadDecimal(context.Reader);
                    context.Buffers[columnIndex].IsNull  = false;
                    break;

                case ProtoDataType.ByteArray:
                    context.Buffers[columnIndex].ByteArray = ProtoReader.AppendBytes(null, context.Reader);
                    context.Buffers[columnIndex].IsNull    = false;
                    break;

                case ProtoDataType.CharArray:
                    context.Buffers[columnIndex].CharArray = context.Reader.ReadString().ToCharArray();
                    context.Buffers[columnIndex].IsNull    = false;
                    break;

                case ProtoDataType.TimeSpan:
                    context.Buffers[columnIndex].TimeSpan = BclHelpers.ReadTimeSpan(context.Reader);
                    context.Buffers[columnIndex].IsNull   = false;
                    break;
                }
            }
        }