使用 NDepend 查找方法的所有用法(包括通过接口)

本文关键字:包括通 接口 用法 查找 NDepend 方法 使用 | 更新日期: 2023-09-27 18:37:04

使用 NDepend,如何找到特定方法或属性的所有直接和间接用途?

特别是,我需要查找通过使用路径某处的接口发生的用法。 谢谢!

使用 NDepend 查找方法的所有用法(包括通过接口)

右键单击 UI 中任意位置的方法,然后选择菜单:选择方法...> ...(直接或间接)使用我会导致代码查询,例如:

from m in Methods 
let depth0 = m.DepthOfIsUsing("NUnit.Core.NUnitFramework+Assert.GetAssertCount()")
where depth0  >= 0 orderby depth0
select new { m, depth0 }

问题是此类查询提供了间接用法,但不查找通过接口(或在基类中声明的重写方法)发生的调用。

希望您可以通过此查询获得您所要求的内容:

// Retrieve the target method by name
let methodTarget = Methods.WithFullName("NUnit.Core.NUnitFramework+Assert.GetAssertCount()").Single()
// Build a ICodeMetric<IMethod,ushort> representing the depth of indirect
// call of the target method.
let indirectCallDepth = 
   methodTarget.ToEnumerable()
   .FillIterative(
       methods => methods.SelectMany(
          m => m.MethodsCallingMe.Union(m.OverriddensBase)))
from m in indirectCallDepth.DefinitionDomain
select new { m, callDepth = indirectCallDepth[m]  }

此查询的两个基石是:

  • 对 FillIterative() 的调用,以递归方式选择间接调用。
  • 对属性 IMethod.OverriddensBase 的调用,顾名思义。对于方法 M,这将返回在基类或接口中声明的所有方法的可枚举值,由 M 覆盖。