Dre*_*kes 8 .net resharper replace pattern-matching
Resharper 5的新模式匹配看起来非常强大,虽然需要一些修补才能弄清楚如何使用它.
对于那些不熟悉此功能的人,它允许您在代码中搜索特定模式.可任选地用替代方案替换这些模式的实例.在IntelliJ中,这被称为结构搜索和替换.它比简单的RegEx搜索/替换功能强大得多.
我想收集人们正在使用的一系列模式,以便我可以更好地学习如何使用此功能.
我建议每个答案包括:
Dre*_*kes 10
.NET 4引入了System.Enum.HasFlag可以整理代码的方法.
之前:
(myValue & MyFlagsEnum.Foo) == MyFlagsEnum.Foo
Run Code Online (Sandbox Code Playgroud)
后:
myValue.HasFlag(MyFlagsEnum.Foo)
Run Code Online (Sandbox Code Playgroud)
XML:
<CustomPatterns>
<Pattern Severity="SUGGESTION">
<Comment>Can condense using 'Enum.HasFlag' method</Comment>
<ReplaceComment>Replace bit matching with 'Enum.HasFlag'</ReplaceComment>
<ReplacePattern>$myValue$.HasFlag($target$)</ReplacePattern>
<SearchPattern><![CDATA[($myValue$ & $target$) == $target$]]></SearchPattern>
<Params />
<Placeholders>
<ExpressionPlaceholder Name="myValue" ExpressionType="System.Enum" ExactType="False" />
<ExpressionPlaceholder Name="target" ExpressionType="System.Enum" ExactType="False" />
</Placeholders>
</Pattern>
</CustomPatterns>
Run Code Online (Sandbox Code Playgroud)
.NET 4引入了System.Diagnostics.Stopwatch.Restart()可以整理代码的方法。
前:
stopwatch.Reset();
stopwatch.Start();
Run Code Online (Sandbox Code Playgroud)
后:
stopwatch.Restart();
Run Code Online (Sandbox Code Playgroud)
XML:
<CustomPatterns>
<Pattern Severity="SUGGESTION">
<Comment>Use Restart method for System.Diagnostics.Stopwatch</Comment>
<ReplaceComment>Use Restart method for System.Diagnostics.Stopwatch</ReplaceComment>
<ReplacePattern>$stopwatch$.Restart();</ReplacePattern>
<SearchPattern><![CDATA[$stopwatch$.Reset();
$stopwatch$.Start();]]></SearchPattern>
<Params />
<Placeholders>
<ExpressionPlaceholder Name="stopwatch" ExpressionType="System.Diagnostics.Stopwatch" ExactType="True" />
</Placeholders>
</Pattern>
</CustomPatterns>
Run Code Online (Sandbox Code Playgroud)