Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find constructors with more than 3 parameters with Resharpers pattern catalogue

Is it possible to create a search pattern in the pattern catalog of Resharper to find all constructors with more than 3 parameters?
If so, how?
My problem is that I don't know how to tell Resharper that only constructor definitions should match.

like image 836
Daniel Hilgarth Avatar asked Jul 27 '11 16:07

Daniel Hilgarth


1 Answers

As far as I know the patterns in Resharper can only be matched within a method. So you couldn't match the constructor declaration.

I just tried the following pattern though:

new $type$($args$)

Where type is a placeholder for a type (who would have guessed?) and args for at least 3 arguments. This indeed finds all uses of at least 3 arguments constructors, but it wouldn't find constructors that are not used, and most importantly, it would find this:

public class MyClass : MyAbstractClass
{
    public MyClass(int foo1, int foo2) : base(foo1, foo2, 0)
    {
        // ...
    }
}

So maybe if you think you're going to have these cases, instead of using Resharper patterns you should try to use regex Find. It can be hard because come to think of it C# syntax is quite complex, but you could get to something...

Edit: I adapted a visual studio regex search for a constructor declaration, recognizes new lines and at least arguments (which can have optional values):

^(:b|\n)*((public|internal|private|protected|static|sealed)(:b|\n)+)+:i(:b|\n)*\((:b|\n)*:i(:b|\n)+:i(:b|\n)*(|\=(:b|\n)*:a*(:b|\n)*)(,(:b|\n)*:i(:b|\n)+:i(:b|\n)*(|\=(:b|\n)*:a*(:b|\n)*))^2(,(:b|\n)*:i(:b|\n)+:i(:b|\n)*(|\=(:b|\n)*:a*(:b|\n)*))*\)

it's ugly mainly because the VS custom regex doesn't have any translation for the standard \w, {2,} and ?.

like image 78
Evren Kuzucuoglu Avatar answered Sep 21 '22 17:09

Evren Kuzucuoglu