[英]Why does ReSharper think MailMessage.From cannot be null?
[Note: This is similar to Compare string to null - Why does Resharper think this is always false? [注意:这类似于将字符串比较为null - 为什么Resharper认为这总是假的? , but from the source, it appears there is no [NotNull]
attribute on MailMessage.From
.] ,但是从源代码[NotNull]
, MailMessage.From
上似乎没有[NotNull]
属性。
Consider this method: 考虑这种方法:
public void Send(MailMessage mailMessage)
{
if (mailMessage.From == null)
mailMessage.From = new MailAddress(Settings.SmtpSettings.From);
_smtpClient.Send(mailMessage);
}
ReSharper 7.1.1 is warning me that mailMessage.From
cannot be null. ReSharper 7.1.1警告我mailMessage.From
不能为null。 I'm completely baffled by this. 我完全被这个困惑了。
mailMessage.From
is a MailAddress
which is a class (not a struct), so I would think it could definitely be null (although I concede it certainly shouldn't be at the time the message is sent). mailMessage.From
是一个MailAddress
,它是一个类(不是结构),所以我认为它绝对可以为null(虽然我承认它肯定不应该在发送消息时)。
Here is an image showing the ReSharper tooltip I'm getting: 这是一张显示我正在获得的ReSharper工具提示的图片:
Any explanation why ReSharper 7.1 thinks mailMessage.From
cannot be null, or is this a bug? 任何解释为什么ReSharper 7.1认为mailMessage.From
不能为null,或者这是一个错误?
Update 更新
So the plot thickens... 情节变得浓厚......
I wrote a couple tests and got unexpected results. 我写了几个测试并得到了意想不到的结果。
This test fails: 此测试失败:
[Test]
public void FromPropertyOfMailMessageCannotBeNull()
{
var message = new MailMessage();
Assert.IsNotNull(message.From);
}
And this one passes: 这一个通过:
[Test]
public void FromPropertyOfMailMessageIsNullIfDefaultConstructorIsUsed()
{
var message = new MailMessage();
Assert.IsNull(message.From);
}
So, it looks like ReSharper is just plain wrong that MailMessage.From
cannot be null. 因此,看起来ReSharper是完全错误的, MailMessage.From
不能为null。
According to the documentation , ReSharper allows NotNullAttribute
to be applied to external APIs (such as the .NET framework itself.) 根据文档 ,ReSharper允许将NotNullAttribute
应用于外部API(例如.NET框架本身)。
In \\Program Files (x86)\\JetBrains\\ReSharper\\v7.1\\Bin\\ExternalAnnotations\\.NETFramework\\System.Net\\4.0.0.0.Nullness.Gen.xml
在\\Program Files (x86)\\JetBrains\\ReSharper\\v7.1\\Bin\\ExternalAnnotations\\.NETFramework\\System.Net\\4.0.0.0.Nullness.Gen.xml
You have: 你有:
<member name="M:System.Net.PeerToPeer.Collaboration.PeerNearMe.AddToContactManager(System.String,System.String,System.Net.Mail.MailAddress)">
<attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
</member>
As you can see, it's adding the NotNullAttribute
to the MailAddress
class. 如您所见,它将NotNullAttribute
添加到MailAddress
类。
It is a bug in ReSharper annotations. 这是ReSharper注释中的一个错误。 In .netFramework 4.0 this property has check for null value in it's setter but non-null value is not gauranted in the default constructor of MailMessage class. 在.netFramework 4.0中,此属性在其setter中检查了null值,但在MailMessage类的默认构造函数中没有非空值。 I've logged an issue for that: http://youtrack.jetbrains.com/issue/RSRP-337152 我已经记录了一个问题:http: //youtrack.jetbrains.com/issue/RSRP-337152
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.