使用Mono.Cecil

时间:2017-03-17 14:09:56

标签: c# .net mono mono.cecil

我正在尝试使用Mono.Cecil提取传递给特定方法的所有参数 在正在构建项目之后运行的后处理脚本中,我能够找到所有方法调用,甚至提取传递给函数的参数的类型。但是,我无法获得实际价值......所以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...
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

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