ndepend

Getting all Messagebox.Show() calls with only a single parameter with a NDepend query


I need a CQLinq query (NDepend) that returns the names of methods that contain a Messagebox.Show() call and their number of parameters like 1 for Messagebox.Show("Hullo").

The output should contain the name of the method with the `Messagebox.Show' call and the number of parameters of the Show() method call.

What I have tried so far did not work due to a type error:

from m in JustMyCode.Methods
let showMethods = m.MethodsCalled.Where(m1 => m1.SimpleName == "Show" && m1.NbParameters > 0)
where showMethods.Count() > 0
select new { Method=m, ShowCount=showMethods.Count(), ShowParameterCount =  showMethods.Select(m1 => new { PCount= m1.NbParameters})}

The error is:

Type {IEnumerable`1<Record`1<UInt16>>} not accepted to type a result argument.
Only IMethod, IField, IType, INamespace, IAssembly, IMember, ICodeElement, ICodeElementParent, ICodeContainer, IIssue and IRule enumerable of one of these types, string, bool, numerics, nullable of bool and numerics, IAbsoluteFilePath and IAbsoluteDirectoryPath are accepted.

Solution

  • What about this code query?

    from m in JustMyCode.Methods
    let showMethods = m.MethodsCalled.Where(m1 => m1.FullNameLike("MessageBox.Show") && m1.NbParameters > 0)
    where showMethods.Any()
    from mCalled in showMethods
    select new { m, mCalled, mCalled.NbParameters }
    

    mCalled.NbParameters is indeed the number of parameters of the Show() method call.