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

标签 c# ndepend cqlinq

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

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

最佳答案

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

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

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

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

// 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()递归地选择间接调用。
  • 对属性(property)的调用IMethod.OverriddensBase ,顾名思义。对于 M 方法,它返回在基类或接口(interface)中声明的所有方法的可枚举,由 M 覆盖。

关于c# - 使用 NDepend 查找方法的所有使用(包括通过接口(interface)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11012315/

相关文章:

c# - 我可以在同一个查询中多次使用一个参数吗?

c# - 在 C# 中创建 Windows 标题栏区域中的选项卡

c# - 如何将引发的异常从C#类传递到C#表单

c# - 对 NDepend 图中的命名空间进行分组

design-patterns - 在NDepend中构建CQL以验证MVVM模式

c# - WPF 高频数据绑定(bind)

c# - 查找单个类的所有依赖项

c# - 如何在CQLinq中进行分组?

.net - 如何指示 NDepend 忽略 EF 类?

cassandra - 是否支持条件是不等式表达式的 Cassandra Update IF 语句?