使用 Mono.Cecil 提取方法调用的参数

Extract arguments of method calls with Mono.Cecil

我正在尝试使用 Mono.Cecil 提取传递给特定方法的所有参数。
在构建项目后立即运行的 post-processing 脚本中,我能够找到所有方法调用,甚至提取传递给函数的参数类型。但是,我无法获得实际值......所以这甚至可以使用 Mono.Cecil,如果是,我需要查看什么值?

这是我当前的代码:

List<MethodDefinition> methods = new List<MethodDefinition> ();
foreach (ModuleDefinition _module in assembly.Modules) {
    foreach (TypeDefinition _type in _module.Types) {
        methods.AddRange (_type.Methods);
    }
}
var uiExtensionMethod = methods
      .Where(m => m.DeclaringType.Name == "SetCustomText").FirstOrDefault();
var instructions = uiExtensionMethod.Body.Instructions;
var count = instructions.Count;
var instructionArray = instructions.ToArray();
for (int i = 0; i < count; i++) {
    if (instructionArray [i] != null) {
        if (instructionArray [i].Operand != null) {
            var paramType = instructionArray [i].Operand.ToString ();
            // So here I have the type of the parameter, but I cannot get the value...
        }
    }
}

好的,所以我找到了解决方法。

问题是,Mono.Cecil确实找到了我的方法调用,但它在文件中处理它们,其中参数已经写入变量,因此无法转换为字符串。

所以我的解决方案是,解析所有以字符串作为操作数的方法,然后检测它们的 NEXT 操作。如果下一个操作是我选择的函数,那么我找到了我正在寻找的字符串:)