简体   繁体   English

为什么ReSharper认为MailMessage.From不能为空?

[英]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工具提示的图片:

ReSharper指示mailMessage.From == null始终为false

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.

相关问题 Mailmessage.from无法正常工作 - Mailmessage.from not working as expected dotnet中MailMessage.From的使用 - usage of MailMessage.From in dotnet 为什么Resharper认为IPrincipal.Identity永远不会为空? - Why does Resharper think IPrincipal.Identity will never be null? 比较字符串为null - 为什么Resharper认为这总是假的? - Compare string to null - Why does Resharper think this is always false? 为什么Resharper认为私有只读变量可以为null? - Why does resharper think a private readonly variable can be null? 为什么 Resharper 认为此代码无法访问 - Why does Resharper Think This Code is Unreachable 为什么Resharper认为具有属性“SomeValue”的内部类隐藏了外部类中具有相同名称的属性? - Why does Resharper think that an inner class with property “SomeValue” hides a property with the same name in the outer class? 为什么 ReSharper 不建议对这两个块使用 null 传播? - Why does ReSharper not suggest using null propagation for both of these blocks? LINQ to SQL为什么在尝试将新对象添加到数据库时认为我的新对象为空? - Why does LINQ to SQL think think that my new object is null when I try to add it to the database? 为什么我无法从“ MimeKit.MimeMessage”转换为“ System.Net.Mail.MailMessage”? - Why am I getting cannot convert from 'MimeKit.MimeMessage' to 'System.Net.Mail.MailMessage'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM