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あるかを知りません。プロジェクトに追加する必要があります。推奨される方法は、ナゲット経由です。

PM> インストール-パッケージJetBrains.Annotations

または、ソースをプロジェクトに直接埋め込むこともできます。

Resharper -> オプション -> コード注釈 -> デフォルトの実装をクリップボードにコピー

次に、それを Annotations.cs などの新しいファイルに貼り付けます。ContractAnnotation定義はそのファイルにあります。ContractAnnotation の公式記事については、こちらを参照してください


以前の回答 (R#7 以外のバージョンの場合)

これは R# でハードコーディングされていますか?

いいえ、Resharper は外部アノテーションを使用してこの機能を提供します。この記事では、メソッドに独自の外部アノテーションを提供するためのソリューションを含め、すべての質問に答える必要がありますIsNullOrWhiteSpace

注: 外部注釈は、参照されたライブラリでのみ機能するようです。参照がプロジェクトからのものである場合、外部注釈は取得されません。これは理想的とは言えません

TempExtensionsという名前のアセンブリに存在する、というクラスに拡張メソッドがあるとします。ClassLibrary1

この場所に新しいファイルを追加する必要があります

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

xml の内容には、次のものが含まれている必要があります。

<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

私が見る唯一の回避策は、関数パラメーターを拡張メソッドに追加することです。

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 に答える
0

As far as i know is this a problem of the resharper. It can not look into methods and verify that in the method the null-check will be done. If u want to avoid this message i think u have to do the null-checking yourself. But i would prefer to ignore the resharper-message in this situation.

于 2012-08-19T13:19:49.047 に答える