自定义 ReSharper 模式 - 使用 String.SomeMethod 代替字符串.某种方法

本文关键字:字符串 方法 SomeMethod 模式 ReSharper 使用 String 自定义 | 更新日期: 2023-09-27 18:33:47

我知道 C# 中的 stringString 之间没有区别(除了为 System.String 添加的using System(。不过,就像在这个 SO-answer 中一样,当我调用 String.SomeMethod 时,我更喜欢使用带有小写s的字符串作为类型,而使用带有大写S的字符串(例如 String.IsNullOrWhitespace(...) ; String.Format(...)String.Empty .

我知道我可以使用 Visual Studio RESHARPER menu -> Option -> Code Inspection -> Custom Patterns -> Add pattern 在 ReShaper 中添加自定义模式。我添加了以下自定义模式:

  • 搜索模式:string.$method$
  • 模式严重性:Show as suggestion
  • 匹配类似的构造:Unchecked
  • 搜索说明: Check if string.SomeMethod is used instead of String.SomeMethod
  • 替换模式:String.$method$
  • 替换后格式:Checked
  • 缩短参考:Unchecked
  • 替换说明:Use String instead of string

在像string.IsNullOrWhitespace(...)这样的行上,它确实给出了建议并将其正确替换为String.IsNullOrWhitespace(...)。然而,问题是它现在也会在它已经正确或我更换它之后给出建议。那么是否可以使搜索模式区分大小写?所以它只会在string.SomeMethod给出建议,而不是在String.SomeMethod给出建议?

编辑:另外,是否可以选择将其应用于整个项目/解决方案,类似于警告Remove unused directives in file-> Remove unused directives in solution(在v8.0或更高版本中(?

自定义 ReSharper 模式 - 使用 String.SomeMethod 代替字符串.某种方法

您获得此行为的原因是stringString是相同的,并且 ReSharper 经常在后台执行类型缩减。SSR就是这样一个地方,其中发生了许多概括和可能的重新安排。长话短说,SSR 是为语法更改而设计的,您(有效地(所做的是执行纯粹的符号更改,实际上不会更改任何人的代码。