[英]How can I identify the arguments that will be passed to a particular IL method call in Mono.Cecil
I am using Mono.Cecil to do some analysis and rewriting on an assembly. 我正在使用Mono.Cecil对程序集进行一些分析和重写。 In some cases this is pretty easy: you can just look right before the call to see where the arguments are loaded:
在某些情况下,这很简单:您可以在调用之前查看,以查看参数的加载位置:
// Math.Pow(2, 4)
IL_0001: ldc.r8 00 00 00 00 00 00 00 40
IL_000A: ldc.r8 00 00 00 00 00 00 10 40
IL_0013: call System.Math.Pow
However, this gets more complicated when the arguments to the function are themselves complex expressions: 但是,当函数的参数本身是复杂的表达式时,这会变得更复杂:
// Math.Pow(2, Math.Abs(Math.Max(17, "123".GetHashCode())));
IL_0001: ldc.r8 00 00 00 00 00 00 00 40
IL_000A: ldc.i4.s 11
IL_000C: ldstr "123"
IL_0011: callvirt System.Object.GetHashCode
IL_0016: call System.Math.Max
IL_001B: call System.Math.Abs
IL_0020: conv.r8
IL_0021: call System.Math.Pow
In this case, a bunch of stuff happens between when the first argument is loaded and when the second argument is loaded. 在这种情况下,在加载第一个参数和加载第二个参数之间会发生一堆事情。 I'm wondering: does Mono.Cecil expose any tools for finding the IL instructions responsible for pushing each argument?
我想知道:Mono.Cecil是否公开了任何工具来查找负责推送每个参数的IL指令?
ILSpy is an open source IL to C# decompiler based on Cecil and it can be also used as a library called ICSharpCode.Decompiler . ILSpy是基于Cecil的C#反编译器的开源IL,它也可以用作名为ICSharpCode.Decompiler的库 。 It can transform IL to "IL AST", which I think is exactly what you're asking for.
它可以将IL转换为“IL AST”,我认为这正是您所要求的。
If you use it like this: 如果你这样使用它:
var decompiled = new ILAstBuilder().Build(method, true, new DecompilerContext(module));
var block = new ILBlock(decompiled);
new ILInlining(block).InlineAllVariables();
Then for the following method (compiled in Release mode): 然后,对于以下方法(在发布模式下编译):
static double F()
{
return Math.Pow(2, Math.Abs(Math.Max(17, "123".GetHashCode())));
}
The variable block
will contain: 变量
block
将包含:
ret(call(Math::Pow, ldc.r8(2), conv.r8(call(Math::Abs, call(Math::Max, ldc.i4(17), callvirt(object::GetHashCode, ldstr("123")))))));
Note that I have no experience using ICSharpCode.Decompiler, so there might be a better way to do this. 请注意,我没有使用ICSharpCode.Decompiler的经验,因此可能有更好的方法来执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.