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

Joe*_* Gö 7 c# ndepend cqlinq

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

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

Pat*_*eam 7

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

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

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

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

// 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]  }
Run Code Online (Sandbox Code Playgroud)

此查询的两个角落是:

  • 调用FillIterative()以递归方式选择间接调用.
  • 正如其名字所示,对IMethod.OverriddensBase属性的调用.对于方法M,它返回在基类或接口中声明的所有方法的可枚举,由M覆盖.