private static List <Instruction> GetInstructions(ModuleDef module, Importer importer, MethodDef method)
        {
            var writeLine = importer.Import(MethodCallHelper.GetDebugWriteLineCall());

            var instructions = new List <Instruction>();

            instructions.Add(Op(OpCodes.Ldstr, GetParametersFormatString(method)));
            instructions.Add(Op(OpCodes.Ldc_I4, method.ParamDefs.Count));
            instructions.Add(Op(OpCodes.Newarr, module.CorLibTypes.Object.TypeRef));

            for (var index = 0; index < method.ParamDefs.Count; index++)
            {
                var parameter = method.Parameters[index + (method.IsStatic ? 0 : 1)];
                instructions.Add(Op(OpCodes.Dup));
                instructions.Add(Op(OpCodes.Ldc_I4, index));
                instructions.Add(Op(OpCodes.Ldarg, parameter));
                if (parameter.Type.IsValueType)
                {
                    instructions.Add(Op(OpCodes.Box, parameter.Type.ToTypeDefOrRef()));
                }
                instructions.Add(Op(OpCodes.Stelem_Ref));
            }
            instructions.Add(Op(OpCodes.Call, writeLine));

            return(instructions);
        }
示例#2
0
        private static List <Instruction> GetInstructions(ModuleDef module, Importer importer, MethodDef method)
        {
            var writeLine   = importer.Import(MethodCallHelper.GetDebugWriteLineCall());
            var resultLocal = new Local(method.ReturnType);

            method.Body.Variables.Add(resultLocal);

            var instructions = new List <Instruction>();

            instructions.Add(Op(OpCodes.Stloc, resultLocal));
            instructions.Add(Op(OpCodes.Ldstr, "Result Type: {0}, Value: {1}"));
            instructions.Add(Op(OpCodes.Ldc_I4, 2));
            instructions.Add(Op(OpCodes.Newarr, module.CorLibTypes.Object.TypeRef));
            instructions.Add(Op(OpCodes.Dup));
            instructions.Add(Op(OpCodes.Ldc_I4, 0));
            instructions.Add(Op(OpCodes.Ldstr, method.ReturnType.FullName));
            instructions.Add(Op(OpCodes.Stelem_Ref));
            instructions.Add(Op(OpCodes.Dup));
            instructions.Add(Op(OpCodes.Ldc_I4, 1));
            instructions.Add(Op(OpCodes.Ldloc, resultLocal));
            if (method.ReturnType.IsValueType)
            {
                instructions.Add(Op(OpCodes.Box, method.ReturnType.ToTypeDefOrRef()));
            }
            instructions.Add(Op(OpCodes.Stelem_Ref));
            instructions.Add(Op(OpCodes.Call, writeLine));
            instructions.Add(Op(OpCodes.Ldloc, resultLocal));

            return(instructions);
        }
        private static List <Instruction> GetInstructions(ModuleDef module, Importer importer)
        {
            var writeLine        = importer.Import(MethodCallHelper.GetDebugWriteLineCall());
            var getCurrentMethod = importer.Import(GetCurrentMethodCall());
            var getNameMethod    = importer.Import(typeof(MemberInfo).GetMethod("get_Name"));

            var instructions = new List <Instruction>();

            instructions.Add(Op(OpCodes.Ldstr, "MethodName: {0}"));
            instructions.Add(Op(OpCodes.Ldc_I4, 1));
            instructions.Add(Op(OpCodes.Newarr, module.CorLibTypes.Object.TypeRef));
            instructions.Add(Op(OpCodes.Dup));
            instructions.Add(Op(OpCodes.Ldc_I4, 0));
            instructions.Add(Op(OpCodes.Call, getCurrentMethod));
            instructions.Add(Op(OpCodes.Callvirt, getNameMethod));
            instructions.Add(Op(OpCodes.Stelem_Ref));
            instructions.Add(Op(OpCodes.Call, writeLine));

            return(instructions);
        }