为什么我需要处置 System.Net.Mail.MailMessage 实例? [英] Why do I need to Dispose a System.Net.Mail.MailMessage instance?
本文介绍了为什么我需要处置 System.Net.Mail.MailMessage 实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它分配了哪些需要处置的非托管资源?它不只是一个简单的托管数据数组吗?那么为什么要处理?
What unmanaged resources does it allocates that needs to be disposed? Isn't it just a simple array of managed data? So why disposing?
推荐答案
邮件消息有附件 -> 附件是 Streams -> Streams 将被处理.
A mail message has attachments -> attachments are Streams -> Streams are to be disposed.
这里是MailMessage的反编译的Dispose方法:
Here is the decompiled Dispose method of MailMessage:
protected virtual void Dispose(bool disposing)
{
if (disposing && !this.disposed)
{
this.disposed = true;
if (this.views != null)
{
this.views.Dispose();
}
if (this.attachments != null)
{
this.attachments.Dispose();
}
if (this.bodyView != null)
{
this.bodyView.Dispose();
}
}
}
作为一般规则,一个类应该实现 IDisposable,如果它包含的任何子类实现它.
As a general rule a class should implement IDisposable if any of its contained children implement it.
这篇关于为什么我需要处置 System.Net.Mail.MailMessage 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文