9

这是带有 Visual Studio 2012 的 ReSharper 7。下面的示例

// This code works fine and as expected and ReShrper is happy with it
if (!string.IsNullOrWhiteSpace(extension) && extension.Length == 3)
{
    // do something
}

// ReSharper highlights "extension" in extension.Length with "Possible 'System.NullReferenceException'"
if (!extension.IsNullOrWhiteSpace() && extension.Length == 3)
{
    // do something
}

而且,我创建了以下扩展方法:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string s)
    {
        return string.IsNullOrWhiteSpace(s);
    }
}

我查看了反映的代码,String.IsNullOrWhiteSpace它没有任何相关代码或属性可以突出显示检查已验证的 R#。这是在 R# 中硬编码的吗?

我查看了 Code Contracts,但我不确定它是否对我有帮助。

您是否有向 ReSharper 证明检查条件已通过我的扩展方法验证的解决方法?

4

3 回答 3

16

在 Resharper 7 及更高版本中可用

[ContractAnnotation("null=>true")]
public static bool IsNullOrWhiteSpace(this string s)

你的项目不会知道是什么ContractAnnotation。您需要将其添加到您的项目中。首选方法是通过 nuget:

PM> 安装包JetBrains.Annotations

或者,您可以直接将源代码嵌入到您的项目中:

Resharper -> 选项 -> 代码注释 -> 将默认实现复制到剪贴板

Then paste that into a new file, eg Annotations.cs. The ContractAnnotation definition lives in that file. For an official article on ContractAnnotation see here


Previous answer (for non R#7 versions)

Is this hardcoded in R#?

No, Resharper uses External Annotations to provide this functionality. This article should answer all your questions, including a solution to provide your own external annotation for your IsNullOrWhiteSpace method.

Example

note: external annotations appear to only work on referenced libraries; if your reference is from a project the external annotations are not picked up; this is less than ideal

Suppose you have your extension method in a class called TempExtensions which itself resides in an assembly named ClassLibrary1

You need to add a new file at this location

C:\Program Files (x86)\JetBrains\ReSharper\v7.0\Bin\ExternalAnnotations.NETFramework.ExternalAnnotations\ClassLibrary1\ClassLibrary1.xml

The contents of the xml should contain:

<assembly name="ClassLibrary1">
  <member name="M:ClassLibrary1.TempExtensions.IsNullOrWhiteSpace(System.String)">
    <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String,System.Boolean)">
        <argument>null=&gt;true</argument>
        <argument>true</argument>
    </attribute>
  </member>
</assembly>
于 2012-08-20T11:32:31.257 回答
0

据我所知,这是 reshaper 的问题。它不能查看方法并验证方法中是否会进行空值检查。如果您想避免此消息,我认为您必须自己进行空值检查。但在这种情况下,我宁愿忽略 resharper-message。

于 2012-08-19T13:19:49.047 回答
0

我看到的唯一解决方法是将函数参数添加到扩展方法。

public static class StringExtensions
{
  public static bool IsNotNullAndNotWhiteSpaceAnd(this string s, Func<string, bool> func)
  {
    if (string.IsNullOrWhiteSpace(s))
    {
      return false;
    }

    return func(s);
  }
}

使用此扩展方法的示例:

string extension = null;     //Test for null
//string extension = "123";  //Test for Length==3


if (extension.IsNotNullAndNotWhiteSpaceAnd(_ => _.Length == 3))
{
  Console.Out.WriteLine("Length == 3");
}
else
{
  Console.Out.WriteLine("Null or WhiteSpace or Length != 3");
}
于 2012-08-20T10:45:46.190 回答