c# - 处置派生类

标签 c# idisposable

我有一个派生自 System.Net.Mail.MailMessage 的类。该类将只包含一些 HTML 格式的电子邮件正文静态文本。

public sealed class CustomMessage : MailMessage
{
    public void SendTo(params string[] addresses)
    {
        foreach (var address in addresses)
        {
            SendTo(address);
        }
    }

    public void SendTo(string address)
    {
        To.Add(address);
    }

    // ...
}

然后我可以将它包装在 using 语句中:

using (var message = new CustomMessage())
{
     message.SendTo("address1", "address2");
     //...
}

MailMessage 基类实现了IDisposable:

protected virtual void Dispose(bool disposing)
{
  if (!disposing || this.disposed)
    return;
  this.disposed = true;
  if (this.views != null)
    this.views.Dispose();
  if (this.attachments != null)
    this.attachments.Dispose();
  if (this.bodyView == null)
    return;
  this.bodyView.Dispose();
}

虽然我的类中没有真正要处置的东西,但我是否仍需要重写 Dispose(bool) 方法?

bool disposed = false;

protected override void Dispose(bool disposing)
{
    if (disposed)
     return; 

     if (disposing) 
     {
        // Nothing managed to dispose.
     }

     // Nothing unmanaged to dispose.

     disposed = true;
     base.Dispose(disposing);
}

最佳答案

不,CustomMessage 继承了基本的 Dispose()Dispose(bool) 方法。它不需要覆盖它们,除非它必须自己做一些额外的处理。

我刚刚注意到您使用 System.Net.Mail.MailMessage。不。它已经过时,Microsoft 本身也强烈警告不要使用它。

自定义消息的替代方法

无论如何,最好编写一个扩展方法来添加多个收件人,而不是创建一个新的消息类。您可以在 MimeKit 中创建类似这样的东西,这是 SmtpClient 的建议替代品:

static public void AddRecipients(this MimeMessage message,IEnumerable<string> addresses)
{
    var ads=addresses.Select(ad=>MailboxAddress.Parse(ad));
    message.To.AddRange(ads);
}

或者这个用于 SmptClient

static public void AddRecipients(this MailMessage message,IEnumerable<string> addresses)
{
    foreach (var address in addresses)
    {
        message.To.Add(address);
    }
}

SmptClient 已过时

我刚刚注意到您使用 System.Net.Mail.MailMessage。不。微软本身warns against using SmptClient在 SmptClient 文档页面顶部的非常强格式警告中:

Warning

This API is now obsolete.

事实上,您应该已经收到编译器警告了:

SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead

您应该更改代码以使用 MailKit。您可能不必首先创建自定义消息。

对于简单的情况,API 类似于 SmptClient,甚至还有一个从 System.Net.Mail.MailMessage 到 MimeKit 自己的 MimeMessage 的转换操作,使转换更容易。

例子来自 MailKit's Github landing page显示使用它是多么容易

var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey Tribbiani", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "chandler@friends.com"));
message.Subject = "How you doin'?";

message.Body = new TextPart ("plain") {
    Text = @"Hey Chandler,

I just wanted to let you know that Monica and I were going to go play some paintball, you in?

-- Joey"
};

using (var client = new SmtpClient ()) {
    // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
    client.ServerCertificateValidationCallback = (s,c,h,e) => true;

    client.Connect ("smtp.friends.com", 587, false);

    // Note: only needed if the SMTP server requires authentication
    client.Authenticate ("joey", "password");

    client.Send (message);
    client.Disconnect (true);
}

关于c# - 处置派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58338818/

相关文章:

c# - 当 MSDN 没有显示时,您如何知道方法返回的内容?

c# - 增加旧值 FluentMigrator

c++-cli - C++ Ref 类不是 System::IDisposable 的成员;实现 IDisposable 时遇到麻烦

c# - 如果我在方法中的 using block 内返回一个值,using 是否会在返回之前处理该对象?

c# - 我负责处理 BackgroundImage 吗?

c# - 这是类层次结构的 "traditional"处置模式的合法替代方案吗?

c# - 具有 Thread 成员的类是否应该实现 IDisposable?

C# Linq问题

c# - FormView 插入新行 C# 和 ASP.NET

c# - Wix C# 自定义操作根本不执行