gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:21:17 29 4
gpt4 key购买 nike

使用 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com