示例#1
0
        // Token: 0x0600040D RID: 1037 RVA: 0x00015408 File Offset: 0x00013608
        internal static IProtoSerializer TryGetCoreSerializer(RuntimeTypeModel model, DataFormat dataFormat, Type type, out WireType defaultWireType, bool asReference, bool dynamicType, bool overwriteList, bool allowComplexTypes)
        {
            Type underlyingType = Helpers.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                type = underlyingType;
            }
            if (Helpers.IsEnum(type))
            {
                if (allowComplexTypes && model != null)
                {
                    defaultWireType = WireType.Variant;
                    return(new EnumSerializer(type, model.GetEnumMap(type)));
                }
                defaultWireType = WireType.None;
                return(null);
            }
            else
            {
                ProtoTypeCode typeCode = Helpers.GetTypeCode(type);
                switch (typeCode)
                {
                case ProtoTypeCode.Boolean:
                    defaultWireType = WireType.Variant;
                    return(new BooleanSerializer(model));

                case ProtoTypeCode.Char:
                    defaultWireType = WireType.Variant;
                    return(new CharSerializer(model));

                case ProtoTypeCode.SByte:
                    defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                    return(new SByteSerializer(model));

                case ProtoTypeCode.Byte:
                    defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                    return(new ByteSerializer(model));

                case ProtoTypeCode.Int16:
                    defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                    return(new Int16Serializer(model));

                case ProtoTypeCode.UInt16:
                    defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                    return(new UInt16Serializer(model));

                case ProtoTypeCode.Int32:
                    defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                    return(new Int32Serializer(model));

                case ProtoTypeCode.UInt32:
                    defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                    return(new UInt32Serializer(model));

                case ProtoTypeCode.Int64:
                    defaultWireType = ValueMember.GetIntWireType(dataFormat, 64);
                    return(new Int64Serializer(model));

                case ProtoTypeCode.UInt64:
                    defaultWireType = ValueMember.GetIntWireType(dataFormat, 64);
                    return(new UInt64Serializer(model));

                case ProtoTypeCode.Single:
                    defaultWireType = WireType.Fixed32;
                    return(new SingleSerializer(model));

                case ProtoTypeCode.Double:
                    defaultWireType = WireType.Fixed64;
                    return(new DoubleSerializer(model));

                case ProtoTypeCode.Decimal:
                    defaultWireType = WireType.String;
                    return(new DecimalSerializer(model));

                case ProtoTypeCode.DateTime:
                    defaultWireType = ValueMember.GetDateTimeWireType(dataFormat);
                    return(new DateTimeSerializer(model));

                case (ProtoTypeCode)17:
                    break;

                case ProtoTypeCode.String:
                    defaultWireType = WireType.String;
                    if (asReference)
                    {
                        return(new NetObjectSerializer(model, model.MapType(typeof(string)), 0, BclHelpers.NetObjectOptions.AsReference));
                    }
                    return(new StringSerializer(model));

                default:
                    switch (typeCode)
                    {
                    case ProtoTypeCode.TimeSpan:
                        defaultWireType = ValueMember.GetDateTimeWireType(dataFormat);
                        return(new TimeSpanSerializer(model));

                    case ProtoTypeCode.ByteArray:
                        defaultWireType = WireType.String;
                        return(new BlobSerializer(model, overwriteList));

                    case ProtoTypeCode.Guid:
                        defaultWireType = WireType.String;
                        return(new GuidSerializer(model));

                    case ProtoTypeCode.Uri:
                        defaultWireType = WireType.String;
                        return(new StringSerializer(model));

                    case ProtoTypeCode.Type:
                        defaultWireType = WireType.String;
                        return(new SystemTypeSerializer(model));
                    }
                    break;
                }
                IProtoSerializer protoSerializer = model.AllowParseableTypes ? ParseableSerializer.TryCreate(type, model) : null;
                if (protoSerializer != null)
                {
                    defaultWireType = WireType.String;
                    return(protoSerializer);
                }
                if (allowComplexTypes && model != null)
                {
                    int key = model.GetKey(type, false, true);
                    if (asReference || dynamicType)
                    {
                        defaultWireType = ((dataFormat == DataFormat.Group) ? WireType.StartGroup : WireType.String);
                        BclHelpers.NetObjectOptions netObjectOptions = BclHelpers.NetObjectOptions.None;
                        if (asReference)
                        {
                            netObjectOptions |= BclHelpers.NetObjectOptions.AsReference;
                        }
                        if (dynamicType)
                        {
                            netObjectOptions |= BclHelpers.NetObjectOptions.DynamicType;
                        }
                        if (key >= 0)
                        {
                            if (asReference && Helpers.IsValueType(type))
                            {
                                string text = "AsReference cannot be used with value-types";
                                if (type.Name == "KeyValuePair`2")
                                {
                                    text += "; please see http://stackoverflow.com/q/14436606/";
                                }
                                else
                                {
                                    text = text + ": " + type.FullName;
                                }
                                throw new InvalidOperationException(text);
                            }
                            MetaType metaType = model[type];
                            if (asReference && metaType.IsAutoTuple)
                            {
                                netObjectOptions |= BclHelpers.NetObjectOptions.LateSet;
                            }
                            if (metaType.UseConstructor)
                            {
                                netObjectOptions |= BclHelpers.NetObjectOptions.UseConstructor;
                            }
                        }
                        return(new NetObjectSerializer(model, type, key, netObjectOptions));
                    }
                    if (key >= 0)
                    {
                        defaultWireType = ((dataFormat == DataFormat.Group) ? WireType.StartGroup : WireType.String);
                        return(new SubItemSerializer(type, key, model[type], true));
                    }
                }
                defaultWireType = WireType.None;
                return(null);
            }
        }
        internal static IProtoSerializer TryGetCoreSerializer(RuntimeTypeModel model, ProtoBuf.DataFormat dataFormat, Type type, out WireType defaultWireType, bool asReference, bool dynamicType, bool overwriteList, bool allowComplexTypes)
        {
            IProtoSerializer protoSerializer;
            Type             underlyingType = Helpers.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                type = underlyingType;
            }
            if (Helpers.IsEnum(type))
            {
                if (!allowComplexTypes || model == null)
                {
                    defaultWireType = WireType.None;
                    return(null);
                }
                defaultWireType = WireType.Variant;
                return(new EnumSerializer(type, model.GetEnumMap(type)));
            }
            ProtoTypeCode typeCode = Helpers.GetTypeCode(type);

            switch (typeCode)
            {
            case ProtoTypeCode.Boolean:
            {
                defaultWireType = WireType.Variant;
                return(new BooleanSerializer(model));
            }

            case ProtoTypeCode.Char:
            {
                defaultWireType = WireType.Variant;
                return(new CharSerializer(model));
            }

            case ProtoTypeCode.SByte:
            {
                defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                return(new SByteSerializer(model));
            }

            case ProtoTypeCode.Byte:
            {
                defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                return(new ByteSerializer(model));
            }

            case ProtoTypeCode.Int16:
            {
                defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                return(new Int16Serializer(model));
            }

            case ProtoTypeCode.UInt16:
            {
                defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                return(new UInt16Serializer(model));
            }

            case ProtoTypeCode.Int32:
            {
                defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                return(new Int32Serializer(model));
            }

            case ProtoTypeCode.UInt32:
            {
                defaultWireType = ValueMember.GetIntWireType(dataFormat, 32);
                return(new UInt32Serializer(model));
            }

            case ProtoTypeCode.Int64:
            {
                defaultWireType = ValueMember.GetIntWireType(dataFormat, 64);
                return(new Int64Serializer(model));
            }

            case ProtoTypeCode.UInt64:
            {
                defaultWireType = ValueMember.GetIntWireType(dataFormat, 64);
                return(new UInt64Serializer(model));
            }

            case ProtoTypeCode.Single:
            {
                defaultWireType = WireType.Fixed32;
                return(new SingleSerializer(model));
            }

            case ProtoTypeCode.Double:
            {
                defaultWireType = WireType.Fixed64;
                return(new DoubleSerializer(model));
            }

            case ProtoTypeCode.Decimal:
            {
                defaultWireType = WireType.String;
                return(new DecimalSerializer(model));
            }

            case ProtoTypeCode.DateTime:
            {
                defaultWireType = ValueMember.GetDateTimeWireType(dataFormat);
                return(new DateTimeSerializer(model));
            }

            case ProtoTypeCode.Unknown | ProtoTypeCode.DateTime:
            {
Label0:
                if (model.AllowParseableTypes)
                {
                    protoSerializer = ParseableSerializer.TryCreate(type, model);
                }
                else
                {
                    protoSerializer = null;
                }
                IProtoSerializer protoSerializer1 = protoSerializer;
                if (protoSerializer1 != null)
                {
                    defaultWireType = WireType.String;
                    return(protoSerializer1);
                }
                if (allowComplexTypes && model != null)
                {
                    int key = model.GetKey(type, false, true);
                    if (asReference || dynamicType)
                    {
                        defaultWireType = (dataFormat == ProtoBuf.DataFormat.Group ? WireType.StartGroup : WireType.String);
                        BclHelpers.NetObjectOptions netObjectOption = BclHelpers.NetObjectOptions.None;
                        if (asReference)
                        {
                            netObjectOption = (BclHelpers.NetObjectOptions)((byte)(netObjectOption | BclHelpers.NetObjectOptions.AsReference));
                        }
                        if (dynamicType)
                        {
                            netObjectOption = (BclHelpers.NetObjectOptions)((byte)(netObjectOption | BclHelpers.NetObjectOptions.DynamicType));
                        }
                        if (key >= 0)
                        {
                            if (asReference && Helpers.IsValueType(type))
                            {
                                string str = "AsReference cannot be used with value-types";
                                str = (type.Name != "KeyValuePair`2" ? string.Concat(str, ": ", type.FullName) : string.Concat(str, "; please see http://stackoverflow.com/q/14436606/"));
                                throw new InvalidOperationException(str);
                            }
                            MetaType item = model[type];
                            if (asReference && item.IsAutoTuple)
                            {
                                netObjectOption = (BclHelpers.NetObjectOptions)((byte)(netObjectOption | BclHelpers.NetObjectOptions.LateSet));
                            }
                            if (item.UseConstructor)
                            {
                                netObjectOption = (BclHelpers.NetObjectOptions)((byte)(netObjectOption | BclHelpers.NetObjectOptions.UseConstructor));
                            }
                        }
                        return(new NetObjectSerializer(model, type, key, netObjectOption));
                    }
                    if (key >= 0)
                    {
                        defaultWireType = (dataFormat == ProtoBuf.DataFormat.Group ? WireType.StartGroup : WireType.String);
                        return(new SubItemSerializer(type, key, model[type], true));
                    }
                }
                defaultWireType = WireType.None;
                return(null);
            }

            case ProtoTypeCode.String:
            {
                defaultWireType = WireType.String;
                if (!asReference)
                {
                    return(new StringSerializer(model));
                }
                return(new NetObjectSerializer(model, model.MapType(typeof(string)), 0, BclHelpers.NetObjectOptions.AsReference));
            }

            default:
            {
                switch (typeCode)
                {
                case ProtoTypeCode.TimeSpan:
                {
                    defaultWireType = ValueMember.GetDateTimeWireType(dataFormat);
                    return(new TimeSpanSerializer(model));
                }

                case ProtoTypeCode.ByteArray:
                {
                    defaultWireType = WireType.String;
                    return(new BlobSerializer(model, overwriteList));
                }

                case ProtoTypeCode.Guid:
                {
                    defaultWireType = WireType.String;
                    return(new GuidSerializer(model));
                }

                case ProtoTypeCode.Uri:
                {
                    defaultWireType = WireType.String;
                    return(new StringSerializer(model));
                }

                case ProtoTypeCode.Type:
                {
                    defaultWireType = WireType.String;
                    return(new SystemTypeSerializer(model));
                }

                default:
                {
                    goto Label0;
                }
                }
                break;
            }
            }
        }