你是如何使用ReSharper的5相匹配的功能模式? [英] How are you using the pattern matching functionality in ReSharper 5?

查看:131
本文介绍了你是如何使用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 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>

这篇关于你是如何使用ReSharper的5相匹配的功能模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆