我可以“修复"吗?错误的 Resharper 注释? [英] Can I "fix" wrong Resharper annotations?

查看:41
本文介绍了我可以“修复"吗?错误的 Resharper 注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 Resharper,但有时它会给出错误的警告,可能是因为 BCL 类型的内置注释是错误的.例如,在这段代码中:

I love Resharper, but sometimes it gives incorrect warnings, probably because the built-in annotations for BCL types are wrong. For instance, in this code:

private static string GetDescription(T value)
{
    Type type = typeof(T);
    string name = Enum.GetName(type, value);
    if (name != null)
    {
        ...

它给了我一个关于 if 语句的警告:表达式始终为真".但是 Enum.GetName 可以返回 null:

It gives me a warning on the if statement: "Expression is always true". But Enum.GetName can return null:

string name = Enum.GetName(typeof(DayOfWeek), (DayOfWeek)42); // null

我认为这是因为 Enum.GetName 有一个 [NotNull] 注释.有没有办法解决这个问题,这样我就不会收到警告?

I assume this is because there is a [NotNull] annotation for Enum.GetName. Is there a way to fix that so I don't get the warning?

注意:我使用的是 Resharper 5.1;也许这个问题在第 6 版中得到了修复,但我现在不愿意升级.

推荐答案

好的,我明白了.内置注释在 Resharper 安装目录(C:\Program Files (x86)\JetBrains\ReSharper\v5.1\Bin\ExternalAnnotations\ 在我的机器上)中的 XML 文件中定义.解决方案是编辑相应的文件以删除或修复不正确的注释.

OK, I got it. The built-in annotations are defined in XML files in the Resharper installation directory (C:\Program Files (x86)\JetBrains\ReSharper\v5.1\Bin\ExternalAnnotations\ on my machine). The solution is to edit the appropriate file to remove or fix the incorrect annotations.

对于 Enum.GetName,要更改的文件是 mscorlib\mscorlib.[version].Contracts.xml.我刚刚评论了这个注释:

In the case of Enum.GetName, the file to change is mscorlib\mscorlib.[version].Contracts.xml. I just commented this annotation:

  <member name="M:System.Enum.GetName(System.Type,System.Object)">
    <attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
  </member>

并重新启动 Visual Studio,现在警告消失了:)

And restarted Visual Studio, and now the warning is gone :)

这篇关于我可以“修复"吗?错误的 Resharper 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆