protected List <Instruction> GetParamInstructions() { /* TRACE ENTRY: * What we'd like to achieve is this: * var paramNames = new string[] { "param1", "param2" } * var paramValues = new object[] { param1, param2 } * log.LogParams(title, paramNames, paramValues) * ...(existing code)... */ var instructions = new List <Instruction>(); var traceEnterNeedsParamArray = body.Method.Parameters.Any(param => !param.IsOut); var traceEnterParamArraySize = body.Method.Parameters.Count(param => !param.IsOut); var stringArrayVar = new VariableDefinition(ModuleDefinition.Import(typeof(string[]))); var objectArrayVar = new VariableDefinition(ModuleDefinition.Import(typeof(object[]))); if (traceEnterNeedsParamArray) { instructions.Add(Instruction.Create(OpCodes.Ldc_I4, traceEnterParamArraySize)); //setArraySize instructions.Add(Instruction.Create(OpCodes.Newarr, String)); //create name array stringArrayVar = body.DeclareVariable("$stringParamArray", stringArrayVar.VariableType); instructions.Add(Instruction.Create(OpCodes.Stloc, stringArrayVar)); //store it in local variable instructions.Add(Instruction.Create(OpCodes.Ldc_I4, traceEnterParamArraySize)); //setArraySize instructions.Add(Instruction.Create(OpCodes.Newarr, Object)); //create name array objectArrayVar = body.DeclareVariable("$objectParamArray", objectArrayVar.VariableType); instructions.Add(Instruction.Create(OpCodes.Stloc, objectArrayVar)); //store it in local variable instructions.AddRange(BuildInstructionsToCopyParameterNamesAndValues( body.Method.Parameters.Where(p => !p.IsOut), stringArrayVar, objectArrayVar, 0)); } else { return(instructions); } //build up logger call if (IsInstanceLogger) { instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); } instructions.Add(Instruction.Create(IsInstanceLogger ? OpCodes.Ldfld : OpCodes.Ldsfld, LoggerField)); instructions.Add(Instruction.Create(OpCodes.Ldstr, "Parameter Values")); instructions.Add(Instruction.Create(OpCodes.Ldloc, stringArrayVar)); instructions.Add(Instruction.Create(OpCodes.Ldloc, objectArrayVar)); instructions.Add(Instruction.Create(OpCodes.Ldc_I4, 40)); instructions.Add(Instruction.Create(OpCodes.Ldstr, "")); instructions.Add(Instruction.Create(OpCodes.Ldstr, "")); instructions.Add(Instruction.Create(OpCodes.Ldnull)); instructions.Add(Instruction.Create(OpCodes.Ldstr, Method.DisplayName())); var lineNumber = 0; Method.Body.Instructions.Last()?.TryGetLineNumber(Method, false, out lineNumber); instructions.Add(Instruction.Create(OpCodes.Ldc_I4, lineNumber)); instructions.Add(Instruction.Create(IsInstanceLogger ? OpCodes.Callvirt : OpCodes.Call, ParamsMethod)); return(instructions); }