[英]Extract arguments of method calls with 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...
}
}
}
好的,所以我找到了解決方案。
問題是,Mono.Cecil確實找到了我的方法調用,但是它在文件中處理了它們,在該文件中,參數已被寫入變量,因此無法轉換為字符串。
所以我的解決方案是,解析所有將字符串作為操作數的方法,然后檢測其NEXT操作。 如果下一個操作是我選擇的功能,那么我找到了要查找的字符串:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.