Want to custom rule around parameter names excluding lambda expressions

Feb 21, 2014 at 5:22 PM

I want to setup a couple of rules around parameter names to apply for Indexers, Methods, Constructors. However, I want for this to exclude such things as Lambda parameter lists.

Currently it seems that the Lambdas are showing as something, possibly as Method (?), I don't know exactly (yet).

How do I get StyleCop to overlook Lambda parameter lists?

Thank ye...
Feb 21, 2014 at 6:19 PM
To illustrate, I want to preclude this scenario, expose it through a StyleCop rule, with "stupidly named" parameters like "a" or "x":
private void Test(int a, string x)
However, in a lambda, I feel it is acceptable to use such a shorthand notation:
Action<int, string> test = (a, x) => { }
That's perfectly fine. Or even like this:
EventHandler<EventArgs> handler = (s, e) => { }
Feb 21, 2014 at 7:39 PM
Edited Feb 21, 2014 at 7:40 PM
I'm not positive this completely addresses the issue. Assuming I've got a Parameter instance in hand:
var parentExpression = parameter.FindParentExpression();

if (parentExpression != null
    && (parentExpression.ExpressionType == ExpressionType.AnonymousMethod
        || parentExpression.ExpressionType == ExpressionType.Lambda))
Assuming that the parentExpression ends up being the Lambda expression.

This prior to checking for things like parameter.Name, and so on.

Thank ye...
Feb 23, 2014 at 2:58 PM
Did a little digging through Google, and discovered that I could be unit testing my rules to bypass the ordeal of having to exit Visual Studio, etc, etc. That would also afford my an opportunity to debug into the various elements, parameters, arguments, etc, to determine how to discover the scope of a parameter, whether what checks are or are not working, and so on. Found a couple of examples I might adopt; the less code I have to write, treat it as a test file, type thing, the better. Don't want to be compiling an entire project, per se, type thing; only processing a single file, type thing.