4

How can I, using CQLINQ, get collection of input arguments for current method? There is any collection like "Arguments" or "Parameters" only "NbParamenter" which is not suitable for my purposes.

1 Answers1

3

Indeed, CQLinq doesn't have this feature yet. However, in many cases you can compensate thanks to the fact that the properties ICodeElement.Name and IMember.FullName, for a IMethod, contain the coma separated list of names of the parameters types. For example here is the FullName of a method:

System.Windows.Forms.Control.BeginInvoke(Delegate,Object[])

and here is its Name:

BeginInvoke(Delegate,Object[])

Here is for example a CQLinq rule that harnesses parameters types names, to match event handlers methods:

// <Name>Event handler methods should be declared private</Name>
warnif count > 0
from m in Application.Methods where 
  !m.IsPrivate &&

   // A method is considered as event handler if...
   m.NbParameters == 2 &&            // ...it has two parameters..
   m.Name.Contains("Object") &&    // ...of types Object...
   m.Name.Contains("EventArgs") && // ...and EventArgs

   // Discard special cases
  !m.ParentType.IsDelegate &&
  !m.IsGeneratedByCompiler

select new { m,m.Visibility }
// This rule implementation relies on the facts that:
// -> A method name contains the type of its parameters.
// -> All EventArgs derived types have the suffix "EventArgs".
Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • 1
    This is fine if you only want the type, but doesn't help if you want to look for specific parameter names :( – Tom Robinson Apr 10 '14 at 12:21
  • Has nDepend 6 resolved this? I wanted to get a list of parameters and their _attributes_ if any. e.g. `[CallerMemberName]`. As tjrobinson mentions, the above code does not seem to help in our case. –  Jun 22 '15 at 06:45
  • Still no such feature in NDepend 6.2.1 with CQLinq 1.9. Pity. – yoyo Mar 09 '16 at 21:58
  • From what I can tell the method name solution doesn't let you look for optional arguments that have been added to methods either. This is unfortunate, as optional arguments can introduce breaking changes to your API and a way to warn against them would be nice. Is there something additional to do? – user2719430 Jul 27 '16 at 19:41
  • No we don't have more method argument support but this will definitely be part of future features – Patrick from NDepend team Jul 29 '16 at 18:58
  • 1
    I need to check where do parameters come from in order to ensure, that the business logic doesn't get polluted with types from another module. So it is allowed to call another module's API and consume its data, but it has to be transformed into local data structures. I'd ideally check if the parameter namespace in a method is from my namespace. Is that possible? This should match the above mentioned feature. – boskicthebrain Jan 15 '18 at 12:36
  • not yet unfortunately, but you can at least check if the parent type of your method(s) is using only certain other types – Patrick from NDepend team Jan 16 '18 at 13:54