在参数中声明 IDisposable 时如何处理它?
How can i dispose IDisposable when it is declared in a parameter?
SendEmail("message", "subject", new System.Net.Mail.Attachment(path1), new System.Net.Mail.Attachment(path2));
如何处理参数中的最后两个附件?完成后它会自行处理吗?
你不能,因为你没有对它们的引用。
将它们移到方法调用之外:
using (var attachment1 = new System.Net.Mail.Attachment(path1))
using (var attachment2 = new System.Net.Mail.Attachment(path2))
{
SendEmail("message", "subject", attachment1, attachment2);
}
SendEmail("message", "subject", new System.Net.Mail.Attachment(path1), new System.Net.Mail.Attachment(path2));
如何处理参数中的最后两个附件?完成后它会自行处理吗?
你不能,因为你没有对它们的引用。
将它们移到方法调用之外:
using (var attachment1 = new System.Net.Mail.Attachment(path1))
using (var attachment2 = new System.Net.Mail.Attachment(path2))
{
SendEmail("message", "subject", attachment1, attachment2);
}