问题
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