Visual Studio 无法识别 IDisposable 类型
Visual Studio not Recognising IDisposable Type
我有以下 Email
class,其中包含 1 个方法 Send
:
using System.Net;
using System.Net.Mail;
static class Email
{
const string EmailAddress = "JoeBloggs@domain.com";
const string Subject = "Error Detected in Application";
public static void Send(string body)
{
using (var client = new SmtpClient("mail.mailserevrname.com", 25)
{ Credentials = new NetworkCredential(), EnableSsl = false })
{
client.Send(EmailAddress,
EmailAddress,
Subject,
body);
}
}
}
我已经在多个应用程序中使用过此代码,并且从未遇到过问题(没有错误,电子邮件已发送 as/when 预期)。
但是,在我当前的项目中,当我复制 class 时,我看到了这个错误:
'SmtpClient': type used in a using statement must be implicitly convertible to 'System.IDisposable'
我从未见过上述代码产生此错误。从the documentation我们也可以看出SmtpClient
确实实现了IDisposable
:
public class SmtpClient : IDisposable
有谁知道为什么这个错误现在出现而不是在我的其他应用程序中?或者有人知道调查这个问题的良好起点吗?
正如@J.Steen 的评论所指出的,在 .NET Framework 3.5 中,SmtpClient
没有实现 IDisposable
.
因此,解决方案是创建一个继承自 SmtpClient
并实现 IDisposable
的 class,并在 using
.[=18= 中使用它]
编辑:
class Mailer: SmtpClient, IDisposable {
//...
}
是实现IDisposable
的方式,继承自SmtpClient
我有以下 Email
class,其中包含 1 个方法 Send
:
using System.Net;
using System.Net.Mail;
static class Email
{
const string EmailAddress = "JoeBloggs@domain.com";
const string Subject = "Error Detected in Application";
public static void Send(string body)
{
using (var client = new SmtpClient("mail.mailserevrname.com", 25)
{ Credentials = new NetworkCredential(), EnableSsl = false })
{
client.Send(EmailAddress,
EmailAddress,
Subject,
body);
}
}
}
我已经在多个应用程序中使用过此代码,并且从未遇到过问题(没有错误,电子邮件已发送 as/when 预期)。
但是,在我当前的项目中,当我复制 class 时,我看到了这个错误:
'SmtpClient': type used in a using statement must be implicitly convertible to 'System.IDisposable'
我从未见过上述代码产生此错误。从the documentation我们也可以看出SmtpClient
确实实现了IDisposable
:
public class SmtpClient : IDisposable
有谁知道为什么这个错误现在出现而不是在我的其他应用程序中?或者有人知道调查这个问题的良好起点吗?
正如@J.Steen 的评论所指出的,在 .NET Framework 3.5 中,SmtpClient
没有实现 IDisposable
.
因此,解决方案是创建一个继承自 SmtpClient
并实现 IDisposable
的 class,并在 using
.[=18= 中使用它]
编辑:
class Mailer: SmtpClient, IDisposable {
//...
}
是实现IDisposable
的方式,继承自SmtpClient