7

使用NDepend,我怎样才能找到特定方法或属性的所有直接和间接用途?

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

4

1 回答 1

7

右键单击 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覆盖。
于 2012-06-13T14:34:20.680 回答