我有ReSharper的规则找到调用Nullable.HasValueReSharper的自定义模式替代负等价式
T? foo;
//...
if(foo.HasValue)
{}
//And it offers to replace with a comparison directly with null:
if(foo != null)
{}
这个伟大的工程,但是当它遇到一个否定.HasValue
,结果是有些奇怪。
if(!foo.HasValue) {}
//is replaced with
if(!(foo != null)) {}
然后ReSharper的要我来简化的声明只是if(foo == null)
//ideally it would automatically get to this without the extra step:
if(foo == null) {}
的规则定义为:
type: System.ValueType or derived
nullable: expression of type System.Nullable<$type$>
search pattern:
$nullable$.HasValue
replace pattern:
$nullable$ != null
( '格式后更换' 和 '缩短引用' 是都检查)
有没有一种方法,我可以写这个规则,所以ReSharper处理它智能?我试着为!$nullable$.HasValue
制定第二条规则,但这会导致两条规则匹配,这使得工具提示建议看起来很混乱:replace with == null
和replace with != null
。
我不知道解决方案。只是一个想法,我想你已经想过了:删除'!foo.HasValue'的模式,只添加'foo.HasValue'和'!(foo!= null)'的模式。因此,一次只有一条规则,但不幸的是,您需要为'!foo.HasValue'等代码使用**两个**连续模式。 – thersch 2012-07-26 08:13:03
搜索模式只包含积极的规则。我已经错过了几次定义否定规则以排除模式的特殊代码的可能性。 – thersch 2012-07-26 08:17:01
resharper自定义模式是一个相当痛苦的工作,我希望有一个测试自定义模式引擎,你可以在那里运行一些已知的好的和已知的坏表情 – Maslow 2013-01-10 14:28:55