Ejemplo n.º 1
0
        public static IEnumerable <Instruction> GetReadValue(TypeReference type, ModuleDefinition module, ILProcessor il, ParameterDefinition readerParameter, ParameterDefinition optionsParameter)
        {
            List <Instruction> ins = new List <Instruction>();

            bool isStandard = IsStandardReadType(type);

            if (!isStandard)
            {
                ins.Add(ILHelper.Ldarg(il, optionsParameter));
                ins.Add(Instruction.Create(OpCodes.Callvirt, module.GetMethod <MessagePackSerializerOptions>("get_Resolver")));
                ins.Add(Instruction.Create(OpCodes.Call, module.GetMethod(typeof(FormatterResolverExtensions), "GetFormatterWithVerify").MakeGenericMethod(type)));
            }

            ins.Add(ILHelper.Ldarg(il, readerParameter));

            if (!isStandard)
            {
                ins.Add(ILHelper.Ldarg(il, optionsParameter));
                ins.Add(Instruction.Create(OpCodes.Callvirt, module.GetMethod(typeof(IMessagePackFormatter <>), "Deserialize").MakeHostInstanceGeneric(module.GetTypeReference(typeof(IMessagePackFormatter <>)).MakeGenericInstanceType(type))));
            }
            else
            {
                ins.Add(GetReadStandardValue(type, module));
            }

            return(ins);
        }
Ejemplo n.º 2
0
        public static Instruction[] GetReadValue(ModuleDefinition module, TypeReference type, ILProcessor il, ParameterDefinition reader, ParameterDefinition options, ParameterDefinition value, VariableDefinition resolverVariable)
        {
            type = module.ImportReference(type);

            List <Instruction> ins = new List <Instruction>();

            bool isStandard = IsStandardReadType(type);

            ins.Add(ILHelper.Ldarg(il, value));

            if (!isStandard)
            {
                if (resolverVariable == null)
                {
                    throw new ArgumentNullException(nameof(resolverVariable), "If you don't provide a getResolver call, you'll need to provide a resolver variable.");
                }

                ins.AddRange(ILHelper.Ldloc(resolverVariable));
                ins.Add(Instruction.Create(OpCodes.Call, module.ImportReference(module.ImportReference(typeof(FormatterResolverExtensions).GetMethod("GetFormatterWithVerify")).MakeGenericMethod(type))));
            }
            else
            {
                ins.Add(ILHelper.Ldarg(il, reader));
            }

            MethodReference readMethod;

            if (type.Is <byte>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadByte));
            }
            else if (type.Is <sbyte>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadSByte));
            }
            else if (type.Is <short>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadInt16));
            }
            else if (type.Is <ushort>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadUInt16));
            }
            else if (type.Is <int>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadInt32));
            }
            else if (type.Is <uint>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadUInt32));
            }
            else if (type.Is <long>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadInt64));
            }
            else if (type.Is <ulong>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadUInt64));
            }
            else if (type.Is <string>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadString));
            }
            else if (type.Is <char>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadChar));
            }
            else if (type.Is <bool>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadBoolean));
            }
            else if (type.Is <float>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadSingle));
            }
            else if (type.Is <double>())
            {
                readMethod = module.GetMethod(typeof(MessagePackReader), nameof(MessagePackReader.ReadDouble));
            }
            else
            {
                ins.Add(ILHelper.Ldarg(il, reader));
                ins.Add(ILHelper.Ldarg(il, options));
                readMethod = module.ImportReference(typeof(IMessagePackFormatter <>).GetMethod("Deserialize")).MakeHostInstanceGeneric(module.ImportReference(typeof(IMessagePackFormatter <>)).MakeGenericInstanceType(type));
            }

            ins.Add(Instruction.Create(isStandard ? OpCodes.Call : OpCodes.Callvirt, readMethod));

            Instruction finish;

            if (type.Is <sbyte>() || type.Is <byte>())
            {
                finish = Instruction.Create(OpCodes.Stind_I1);
            }
            else if (type.Is <short>() || type.Is <ushort>())
            {
                finish = Instruction.Create(OpCodes.Stind_I2);
            }
            else if (type.Is <int>() || type.Is <uint>())
            {
                finish = Instruction.Create(OpCodes.Stind_I4);
            }
            else if (type.Is <long>() || type.Is <ulong>())
            {
                finish = Instruction.Create(OpCodes.Stind_I8);
            }
            else if (type.Is <float>())
            {
                finish = Instruction.Create(OpCodes.Stind_R4);
            }
            else if (type.Is <double>())
            {
                finish = Instruction.Create(OpCodes.Stind_R8);
            }
            else if (type.IsValueType)
            {
                finish = Instruction.Create(OpCodes.Stobj, type);
            }
            else
            {
                finish = Instruction.Create(OpCodes.Stind_Ref);
            }

            ins.Add(finish);

            return(ins.ToArray());
        }