我喜欢 Resharper,但有时它会给出不正确的警告,可能是因为 BCL 类型的内置注释是错误的。例如,在这段代码中:
private static string GetDescription(T value)
{
Type type = typeof(T);
string name = Enum.GetName(type, value);
if (name != null)
{
...
它在 if
语句上给了我一个警告:“Expression is always true”。但是 Enum.GetName
可以返回 null:
string name = Enum.GetName(typeof(DayOfWeek), (DayOfWeek)42); // null
我假设这是因为 Enum.GetName
有一个 [NotNull]
注释。有没有办法解决这个问题,这样我就不会收到警告?
注意:我使用的是 Resharper 5.1;也许该问题已在版本 6 中修复,但我现在不愿意升级。
最佳答案
好的,我知道了。内置注释在 Resharper 安装目录(C:\Program Files (x86)\JetBrains\ReSharper\v5.1\Bin\ExternalAnnotations\
在我的机器上)的 XML 文件中定义。解决方案是编辑适当的文件以删除或修复不正确的注释。
在 Enum.GetName
的情况下,要更改的文件是 mscorlib\mscorlib.[version].Contracts.xml
。我刚刚评论了这个注释:
<member name="M:System.Enum.GetName(System.Type,System.Object)">
<attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
</member>
然后重新启动 Visual Studio,现在警告消失了:)
关于annotations - 我可以 "fix"错误的 Resharper 注释吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10254845/