Find all uses of a method (including via interface) using NDepend

廉价感情. 提交于 2019-12-22 05:01:01

问题


Using NDepend, how can I find all, direct and indirect, uses of a specific method or property?

In particular, I need to find usages that occur via an interface somewhere along the use path. Thanks!


回答1:


Right clicking a method anywhere in the UI, and selecting the menu: Select Method... > ...that are using me (directly or indirectly) leads to a code query like:

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

The problem is that such query gives indirect usage, but doesn't look for calls that occurs via an interface (or an overridden method declared in a base class).

Hopefully what you are asking for can be obtained with this query:

// 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]  }

The two corner stones of this query are:

  • The call to FillIterative() to select recursively the indirect call.
  • The call to the property IMethod.OverriddensBase, as its name suggests. For a method M this returns the enumerable of all method declared in a base class or an interface, overriden by M.


来源:https://stackoverflow.com/questions/11012315/find-all-uses-of-a-method-including-via-interface-using-ndepend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!