Mono.Cecil - simple example how to get method body

爱⌒轻易说出口 提交于 2020-08-05 06:36:29

问题


I have been searching for a newbie question, but can't find a simple example. Can anyone give me a simple example how to get MethodBody into most available string result? Like:

using Mono.Cecil;
using Mono.Cecil.Cil;

namespace my
{
    public class Main
    {
        public Main()
        {
             // phseudo code, but doesnt work
            Console.Write(    getMethod("HelloWorld").GetMethodBody().ToString()   );
        }

        public void HelloWorld(){
             MessageBox.Show("Hiiiiiiiiii");
        }
    }
}

回答1:


Start with reading your assembly:

var path = "... path to your assembly ...";
var assembly = AssemblyDefinition.ReadAssembly(path);

You can use System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName as path if you want to open running process

Now get all types and methods wich you want to inspect

var toInspect = assembly.MainModule
  .GetTypes()
  .SelectMany(t => t.Methods.Select(m => new {t, m}))
  .Where(x => x.m.HasBody);

If you already knew type and method names you can modify your query

toInspect = toInspect.Where(x => x.t.Name.EndsWith("Main") && x.m.Name == "HelloWorld");

After that just iterate over that collection:

foreach (var method in toInspect)
{
    Console.WriteLine($"\tType = {method.t.Name}\n\t\tMethod = {method.m.Name}");
    foreach (var instruction in method.m.Body.Instructions)
        Console.WriteLine($"{instruction.OpCode} \"{instruction.Operand}\"");
}

Output will be

Type = Main
  Method = HelloWorld

ldstr "Hiiiiiiiiii"
call "System.Windows.Forms.DialogResult System.Windows.Forms.MessageBox::Show(System.String)"
pop ""
ret ""


来源:https://stackoverflow.com/questions/49923757/mono-cecil-simple-example-how-to-get-method-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!