我希望有人能帮助我解决我的问题。目前我正在使用 C# 并使用库 Mono.Cecil 在方法退出之前添加指令代码。有一种情况,当if 语句位于方法的末尾并且我想添加TraceUtil.TraceMethodExit(b); 最后,它出现在 if 语句中,但最后应该在外面。我用哪个编译器的指令一模一样?产生,当我手动将TraceUtil.TraceMethodExit(b); 在最后。但我的最终还是出现在 if 语句体中。
但它应该是这样的:
这里有什么编译器?产生:
IL_0000: nop
IL_0001: ldarg.1
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: brfalse.s IL_0013
IL_0006: nop
IL_0007: ldstr "TEST"
IL_000c: call System.Void UnityEngine.Debug::Log(System.Object)
IL_0011: nop
IL_0012: nop
IL_0013: ldnull
IL_0014: call System.Void SEE.Utils.TraceUtil::TraceMethodExit(System.Object[])
IL_0019: nop
IL_001a: ret
以下是我添加说明时的输出:
IL_0000: nop
IL_0001: ldarg.1
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: brfalse.s IL_001a
IL_0006: nop
IL_0007: ldstr "TEST"
IL_000c: call System.Void UnityEngine.Debug::Log(System.Object)
IL_0011: nop
IL_0012: nop
IL_0013: ldnull
IL_0014: call System.Void SEE.Utils.TraceUtil::TraceMethodExit(System.Object[])
IL_0019: nop
IL_001a: ret
这是添加附加说明的代码/方法:
private static void TraceZeroParameterMethodExit(ILProcessor iLProc, Instruction instr, MethodReference mRef)
{
MethodBodyRocks.SimplifyMacros(iLProc.Body);
iLProc.InsertBefore(instr, Instruction.Create(OpCodes.Ldnull));
iLProc.InsertBefore(instr, Instruction.Create(OpCodes.Call, mRef));
iLProc.InsertBefore(instr, Instruction.Create(OpCodes.Nop));
MethodBodyRocks.OptimizeMacros(iLProc.Body);
}
有谁知道如何解决这个问题?也许它与偏移量有关?谢谢你的帮助!