在 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=>true</argument>
<argument>true</argument>
</attribute>
</member>
</assembly>