使用 Resharpers 模式目录查找具有 3 个以上参数的构造函数

发布于 2024-11-26 21:36:33 字数 113 浏览 2 评论 0原文

是否可以在 Resharper 的模式目录中创建搜索模式来查找所有具有 3 个以上参数的构造函数?
如果是这样,怎么办?
我的问题是我不知道如何告诉 Resharper 只有构造函数定义应该匹配。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2024-12-03 21:36:33

据我所知,Resharper 中的模式只能在方法内进行匹配。所以你无法匹配构造函数声明。

我只是尝试了以下模式:

new $type$($args$)

其中 type 是类型的占位符(谁会猜到?),args 至少有 3 个参数。
这确实找到了至少 3 个参数构造函数的所有使用,但它不会找到未使用的构造函数,最重要的是,它会找到这个:

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

所以也许如果你认为你会遇到这些情况,您应该尝试使用正则表达式查找,而不是使用 Resharper 模式。这可能很难,因为想想 C# 语法相当复杂,但你可以得到一些东西......

编辑:我改编了 Visual Studio 正则表达式搜索构造函数声明,识别新行和至少参数(可以有可选值):

^(: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)*))*\)

它很难看,主要是因为 VS 自定义正则表达式没有对标准 \w{2,}? 进行任何翻译>。

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 ?.

调妓 2024-12-03 21:36:33

我知道这个问题是专门针对 R#6.0 的,但我想为 R# 2016.3 提供答案,因为我最近也有同样的问题。

Resharper 2016(.3.1)


Search with Pattern 允许这种类型的搜索。您需要执行以下操作:

选择“Resharper”>“查找>使用模式搜索

添加三个占位符:

  • args:参数占位符 - 将最小/最大设置为要搜索的参数数量
  • 代码:语句占位符 - 对数量不设置限制语句
  • 类型:标识符占位符 - 不输入正则表达式

确保将搜索模式设置为 C#

将模式设置为以下内容:

public $type$($args$) 
{
$code$
}

设置查找范围根据需要(例如 解决方案)。然后点击查找。

您最终应该得到一个如下所示的搜索框:

Search With Pattern

这将找到包含您感兴趣的参数数量的所有(公共)构造函数。我能够在多个解决方案中成功使用它。作为一个额外的好处,似乎很高兴找到多行(而不仅仅是单行)带有参数列表的构造函数。

I know this question is specifically targetted at R#6.0, but I wanted to provide an answer for R# 2016.3, as I had the same question recently.

Resharper 2016(.3.1)


Search with Pattern allows this type of searching. Here's what you need to do:

Select Resharper > Find > Search With Pattern

Add Three Placeholders:

  • args: Argument Placeholder - set the min/max to how many params you want to search for
  • code: Statement Placeholder - set no limit on number of statements
  • type: Identifier Placeholder - enter no regex

Ensure that you set the Search Pattern to C#

Set the pattern to the following:

public $type$($args$) 
{
$code$
}

Set Look In as you need to (e.g. Solution). And hit Find.

You should end up with a search box that looks like this:

Search With Pattern

This will find all (public) constructors which contain the number of params you are interested in. I was able to use this within several solutions successfully. As an added bonus, it seems to be quite happy to find constructors with param lists over multiple lines, not just a single line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文