为什么 System.Net.Mail.MailMessage 实现 IDisposable
因此,我已经使用 System.Net.Mail.MailMessage 对象通过 SmtpClient
发送电子邮件有一段时间了。我注意到 MailMessage
实现了 IDisposable
,因此我总是在 using
块中使用它。
using(MailMessage msg = new MailMessage())
{
msg.To = blah... etc;
...
smtpclient.Send(msg);
}
从元数据中,您可以看到有关 MailMessage
实现的信息,
// Summary:
// Releases all resources used by the System.Net.Mail.MailMessage.
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Dispose();
//
// Summary:
// Releases the unmanaged resources used by the System.Net.Mail.MailMessage
// and optionally releases the managed resources.
//
// Parameters:
// disposing:
// true to release both managed and unmanaged resources; false to release only
// unmanaged resources.
protected virtual void Dispose(bool disposing);
但我想知道,为什么 MailMessage
实现 IDisposable
?它似乎与网络相关的项目没有任何关系,因为 SmtpClient
处理所有这些。
是否可能是由于可能保留附加文件的文件句柄?还有什么我忘记的吗?
So I've been using System.Net.Mail.MailMessage
objects for sending e-mail via SmtpClient
for a while now. I noticed somewhere that MailMessage
implements IDisposable
, so I always use it within a using
block.
using(MailMessage msg = new MailMessage())
{
msg.To = blah... etc;
...
smtpclient.Send(msg);
}
From metadata, you can see this info on the implementation of MailMessage
// Summary:
// Releases all resources used by the System.Net.Mail.MailMessage.
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Dispose();
//
// Summary:
// Releases the unmanaged resources used by the System.Net.Mail.MailMessage
// and optionally releases the managed resources.
//
// Parameters:
// disposing:
// true to release both managed and unmanaged resources; false to release only
// unmanaged resources.
protected virtual void Dispose(bool disposing);
But I'm wondering, why does MailMessage
implement IDisposable
? It does not appear to have anything to do with network-related items, because the SmtpClient
handles all that.
Could it be due to potentially holding file handles for attached files? Is there something else there I'm forgetting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据 dotPeek 称,它正在处理其附件及其视图:
According to dotPeek, it is disposing of its attachments and its views:
它实现 IDisposable,因为它有实现 IDisposable 的子级。例如,附件是一次性对象,因为附件可以是大多数时候需要处理的流。因此,在消息发送出去后,需要处理消息来处理附件(包含流)。
It implements IDisposable because it has children that implement IDisposable. For instance, Attachment is a disposable object because an attachment can be a Stream, which most of the time needs disposing. So, after the message has been sent off, disposal of the message is required for disposal of the attachment (which holds a stream).
MailMessage
类型有几个它拥有的字段,并且实现了IDisposable
。正确实现 IDisposable 模式要求它也实现 IDisposable 并将调用链接到这些字段。特别是附件、视图和正文视图The
MailMessage
type has several fields which it owns and that implementIDisposable
. Proper implementation of theIDisposable
pattern requires that it also implementsIDisposable
and chains the call to those fields. In particular attachments, views, and body view如果您提供图像或附件,则需要在处置时对其进行清理。因此,您应该在 using 中隐式调用 dispose 或显式调用 dispose。
通常,始终对实现 IDisposable 的任何对象调用 dispose。如果没有必要,他们不会实施它。
If you supply images or attachments, then those need to be cleaned up on disposal. As such, calling dispose either implicitly in a using or explicitly is something you should do.
In general, always call dispose on any object that implements IDisposable. They wouldn't have implemented it if it wasn't necessary.
您可以查看原始源代码以了解它当前的具体功能。请参阅 MailMessage.Dispose。我没有在这里包含源代码,因为我不知道它是否被允许。
You can view the original source code to see exactly what it currently does. See MailMessage.Dispose. I haven't included the source code here because I don't know if it's allowed.