你是如何使用ReSharper的5相匹配的功能模式?你是、如何使用、相匹配、模式

由网友(卑微的等待苍老了谁)分享简介:ReSharper的5新的模式匹配看起来很强大,但它需要多一点修修补补的工作,如何使用它。Resharper 5's new pattern matching seems very powerful, though it takes a bit of tinkering to work out how to use...

ReSharper的5新的模式匹配看起来很强大,但它需要多一点修修补补的工作,如何使用它。

Resharper 5's new pattern matching seems very powerful, though it takes a bit of tinkering to work out how to use it.

对于那些谁不熟悉这个功能,它可以让你搜索在你的code特定的模式。这样的图案的实例可任选地被替换为另一种选择。在的IntelliJ这被称为结构性搜索和替换。它比简单更强大的正则表达式查找/替换。

For those who aren't familiar with this feature, it allows you to search for specific patterns within your code. Instances of such patterns may optionally be replaced with an alternative. In IntelliJ this was called structural search and replace. It's much more powerful than simple RegEx search/replace.

我想收集一系列的人使用,这样我可以学习如何使用此功能更好的模式。

I'd like to collect a series of patterns that people are using so that I can learn how to use this feature better.

我建议每个答案包括:

在简要介绍了基本原理的模式 的是什么将匹配一个例子 替换的可选例子 通过导出模式生成的XML,以便其他人可以试一下太

推荐答案

.NET 4中引入了可整理你的code中的 System.Enum.HasFlag 方法。

Matching a [Flags] enum bit

.NET 4 introduces the System.Enum.HasFlag method which can tidy up your code.

在的

(myValue & MyFlagsEnum.Foo) == MyFlagsEnum.Foo

在的

myValue.HasFlag(MyFlagsEnum.Foo)

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>
阅读全文

相关推荐

最新文章