Find direct & indirect method usages of method 在 base class 中被覆盖

Find direct & indirect method usages if method is overriden in base class

请帮我弄清楚如何编写查询:)

密码是:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            var man = new Man("Joe");

            Console.WriteLine(man.ToString());
        }
    }

    public class SuperMan
    {
        public SuperMan(string name)
        {
            this.name = name;
        }

        public override string ToString()
        {
            return name;
        }

        string name;
    }

    public class Man : SuperMan
    {
        public Man(string name) : base(name)
        {
        }
    }
}

我想找到 Man.ToString() 的所有直接和间接依赖项(方法)。 Main()方法只有一次调用。

我正在尝试的查询是:

from m in Methods 
let depth0 = m.DepthOfIsUsing("ConsoleApplication1.SuperMan.ToString()")
where depth0  >= 0 orderby depth0
select new { m, depth0 }.

但它没有找到相关的 Program.Main() 方法....

如何修改查询以找到此类方法的用法?

首先让我们看看直接来电者。我们想要列出所有调用 SuperMan.ToString() 的方法或任何被 SuperMan.ToString() 覆盖的 ToString() 方法。它看起来像:

let baseMethods = Application.Methods.WithFullName("ConsoleApplication1.SuperMan.ToString()").Single().OverriddensBase
from m in Application.Methods.UsingAny(baseMethods)
where m.IsUsing("ConsoleApplication1.Man")  // This filter can be added
select new { m, m.NbLinesOfCode }

注意我们放置了一个过滤子句,因为在现实世界中几乎每个方法都会调用 object.ToString()(这是一个特殊情况)。

现在处理间接调用更加棘手。我们需要在通用序列上调用魔术 FillIterative() 扩展方法。

let baseMethods = Application.Methods.WithFullName("ConsoleApplication1.SuperMan.ToString()").Single().OverriddensBase
let recursiveCallers = baseMethods.FillIterative(methods => methods.SelectMany(m => m.MethodsCallingMe))

from pair in recursiveCallers 
let method = pair.CodeElement
let depth = pair.Value
where method.IsUsing("ConsoleApplication1.Man") // Still same filter
select new { method , depth }

瞧瞧!