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

7
使用NDepend,我该如何找到特定方法或属性的所有直接和间接用途?
特别是,我需要找到发生在使用路径中某个接口上的使用。谢谢!
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覆盖的方法的枚举。

1
能否从此查询结果生成图表?我遇到的问题是覆盖用法未显示... - ironic
1
目前图上的边仅显示“真实”的依赖关系,这种情况将会发展。 - Patrick from NDepend team

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