C# 使用负等式表达式的resharper自定义模式替换

C# 使用负等式表达式的resharper自定义模式替换,c#,resharper,C#,Resharper,我在resharper中有一条规则,可以查找对Nullable.HasValue的调用 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(!(fo

我在resharper中有一条规则,可以查找对Nullable.HasValue的调用

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)

该规则的定义如下:

type:     System.ValueType or derived
nullable: expression of type System.Nullable<$type$>

search pattern:
$nullable$.HasValue

replace pattern:
$nullable$ != null
类型:System.ValueType或派生
nullable:System.nullable类型的表达式
搜索模式:
$nullable$.HasValue
替换模式:
$nullable$!=无效的
(“替换后的格式”和“缩短引用”均已选中)


有没有一种方法可以编写此规则,以便ReSharper能够智能地处理它?我尝试为
制定第二条规则$可为null的$.HasValue
,但这会导致两个规则匹配,这使得工具提示建议看起来很混乱:
replace-with==null
replace-with!=空

如果不是强制性的,你可以放弃这条规则,因为根据这条规则:


“编译器通过调用HasValue来替换空比较,因此没有真正的区别。只需选择可读性更好/对您和您的同事更有意义的选项即可。”

我不知道解决方案。只是一个想法,我想你已经考虑过了:删除
的模式!foo.HasValue
并仅为
foo.HasValue
添加模式!(foo!=null)
。所以一次只有一条规则,但不幸的是,对于像
这样的代码,您需要使用两个连续的模式!foo.HasValue
。搜索模式只包含肯定规则。我已经多次错过了定义否定规则以排除模式特殊代码的可能性。Resharper自定义模式很难解决,我希望有一个测试自定义模式引擎,您可以运行一些已知的好的和已知的坏的表达式,但我的动机是,我认为空比较更具可读性,我希望resharper执行这种风格的决定。
type:     System.ValueType or derived
nullable: expression of type System.Nullable<$type$>

search pattern:
$nullable$.HasValue

replace pattern:
$nullable$ != null