如何使用反射在C#中获取从方法调用的方法列表

7
如何使用反射在C#(DotNet)中获取从方法调用的方法列表或如何使用反射检查是否从Method2调用了Method1?

2
你不能使用反射来做那件事。反射的作用是提供元数据,你需要使用反编译器和/或代码分析来深入挖掘源代码。它可以告诉你房子的颜色、大小和地址,但无法告诉你里面有什么家具。你想要实现什么? - D Stanley
这个有用吗?http://www.codeproject.com/Articles/14058/Parsing-the-IL-of-a-Method-Body - pm100
1个回答

14

正如其他人所指出的那样,使用反射实现这个功能基本上是不可能的。你必须要自己解析方法的IL字节码才能找到调用。幸运的是,有一个名为Mono Cecil的项目非常完美地解决了这个问题,它也可以在nuget上获得。以下是一个极简示例,以说明如何使用Mono Cecil解决你的问题:

static class MethodDefinitionExtensions
{
    public static bool CallsMethod(this MethodDefinition caller, 
        MethodDefinition callee)
    {
        return caller.Body.Instructions.Any(x => 
            x.OpCode == OpCodes.Call && x.Operand == callee);
    }
}

class Program
{
    private static AssemblyDefinition _assembly = AssemblyDefinition.ReadAssembly(
        System.Reflection.Assembly.GetExecutingAssembly().Location);

    private static void Method1()
    {
        Method2();
    }

    private static void Method2()
    {
        Method1();
        Method3();
    }

    private static void Method3()
    {
        Method1();
    }

    private static IEnumerable<MethodDefinition> GetMethodsCalled(
        MethodDefinition caller)
    {
        return caller.Body.Instructions
            .Where(x => x.OpCode == OpCodes.Call)
            .Select(x => (MethodDefinition)x.Operand);
    }

    private static MethodDefinition GetMethod(string name)
    {
        TypeDefinition programType = _assembly.MainModule.Types
            .FirstOrDefault(x => x.Name == "Program");
        return programType.Methods.First(x => x.Name == name);
    }

    public static void Main(string[] args)
    {
        MethodDefinition method1 = GetMethod("Method1");
        MethodDefinition method2 = GetMethod("Method2");
        MethodDefinition method3 = GetMethod("Method3");

        Debug.Assert(method1.CallsMethod(method3) == false);
        Debug.Assert(method1.CallsMethod(method2) == true);
        Debug.Assert(method3.CallsMethod(method1) == true);

        Debug.Assert(GetMethodsCalled(method2).SequenceEqual(
            new List<MethodDefinition> { method1, method3 }));
    }
}

GetMethodsCalled should cast to MethodReference instead of to MethodDefinition - Jan
在调用方法时,是否也可以检查参数的类型? - Mr Patience

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接