ReSharper的自定义模式替代负平等的表达 [英] resharper custom pattern replacement with negative equality expression
问题描述
我在ReSharper的规则,找到Nullable.HasValue电话
I have a rule in resharper to find calls to Nullable.HasValue
T? foo;
//...
if(foo.HasValue)
{}
//And it offers to replace with a comparison directly with null:
if(foo != null)
{}
这个伟大的工程,但是当它遇到一个否定 .HasValue
,结果是有点怪怪的。
This works great, but when it encounters a negated .HasValue
, the result is a little strange.
if(!foo.HasValue) {}
//is replaced with
if(!(foo != null)) {}
然后ReSharper的要我来简化的声明,只是如果(富== NULL)
Then resharper wants me to simplify the statement to just 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
('格式化后替换和缩短引用'都是选中)
('Format after replace' and 'Shorten references' are both checked)
有没有办法让ReSharper的处理它聪明,我可以写这个规则?我试图使第二个规则 $ $可空.HasValue
,但导致这两个规则相匹配,这使得工具提示的建议看起来很奇怪:<!code>更换与== NULL 和与!= NULL
更换。
Is there a way I can write this rule so ReSharper handles it intelligently? I tried making a second rule for !$nullable$.HasValue
, but that causes both rules to match which makes tool-tip suggestions look confusing: replace with == null
and replace with != null
.
推荐答案
如果不是真正强制你可以在这个规则,因为放弃了,按照这个的post :
If is not really mandatory you can give up on this rule because, according to this post:
编译器代替空的比较一起的HasValue电话,所以没有真正的区别。只要做到取其更可读/更有意义,你和你的同事。
"The compiler replaces null comparisons with a call to HasValue, so there is no real difference. Just do whichever is more readable/makes more sense to you and your colleagues."
这篇关于ReSharper的自定义模式替代负平等的表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!