我制作了一个小型 SmtpSender 类来处理 Smtp MailMessage 对象的发送。当消息已发送或发送失败时,我会引发一个委托,该委托包含一个“响应”对象,该对象具有用户尝试发送的原始 MailMessage 以及成功/失败布尔值和错误字符串。然后,用户可以根据需要将 MailMessage 对象重新提交给发件人类以重试。
我想知道的是......如果我提出一个包含非托管资源对象的委托,我是否需要在当前范围内处理该对象?如果是这样,在当前范围内调用 Dispose 会杀死委托函数接收的对象吗?从长远来看,我担心内存泄漏。
任何建议或帮助将不胜感激。提前致谢!
戴夫
public delegate void SmtpSenderSentEventHandler(object sender, SmtpSendResponse theResponse);
public class SmtpSendResponse : IDisposable
{
#region Private Members
private MailMessage _theMessage;
private bool _isSuccess;
private string _errorMessage;
#endregion
#region Public Properties
public MailMessage TheMessage
{
get { return _theMessage; }
set { _theMessage = value; }
}
public bool IsSuccess
{
get { return _isSuccess; }
set { _isSuccess = value; }
}
public string Error
{
get { return _errorMessage; }
set { _errorMessage = value; }
}
#endregion
#region Constructors
public SmtpSendResponse(MailMessage theMessage, bool isSuccess)
: this(theMessage, isSuccess, null)
{ }
public SmtpSendResponse(MailMessage theMessage, bool isSuccess, string errorMessage)
{
_theMessage = theMessage;
_isSuccess = isSuccess;
_errorMessage = errorMessage;
}
#endregion
#region IDisposable Members
public void Dispose()
{
if (_theMessage != null)
{
_theMessage.Attachments.Dispose();
_theMessage.Dispose();
}
}
#endregion
}