C# - 如何在 if 语句中使用无效方法

C# - How to use a voided method in an if statement

我正在编写一个程序,向用户发送电子邮件持有信息。我想要完成的是,如果电子邮件发送成功,那么它将写入电子邮件发送成功的日志文件。我唯一的问题是,我发现很难让 if 语句执行此操作。我知道我不能将该方法放入 if 语句中,因为您不能将 voided 方法转换为布尔值,但还有什么其他方法可以做到这一点?下面是我尝试使用 SendEmail 方法的代码。

SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine);
    if (SendEmail.Success())
     {
       BOAssistant.WriteLine                 
     }

这是方法:

private static void SendEmail(string emailBody, string emailSubject)
     {
        //This is the method that will create the email for you
        Email email = new Email();
        email.To.Add("POvermyer@TandT.com");
        email.Subject = emailSubject;
        email.Body = emailBody;
        email.Send();
    }
if (SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine))
{
    BOAssistant.WriteLine                 
}

...

private static bool SendEmail(string emailBody, string emailSubject)
 {
    //This is the method that will create the email for you
    Email email = new Email();
    email.To.Add("POvermyer@TandT.com");
    email.Subject = emailSubject;
    email.Body = emailBody;
    try{
    email.Send();
    } catch(Exception e) { return false; }
    return true;
}

bool isSent;
SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine, out isSent);
if (isSent)
{
    BOAssistant.WriteLine                 
}

...

private static void SendEmail(string emailBody, string emailSubject, out bool isSent)
 {
    //This is the method that will create the email for you
    Email email = new Email();
    email.To.Add("POvermyer@TandT.com");
    email.Subject = emailSubject;
    email.Body = emailBody;
    try{
    email.Send();
    } catch(Exception e) { isSent = false; }
    isSent = true;
}

SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine, () => BOAssistant.WriteLine);

...

private static void SendEmail(string emailBody, string emailSubject, Action onSuccess)
 {
    //This is the method that will create the email for you
    Email email = new Email();
    email.To.Add("POvermyer@TandT.com");
    email.Subject = emailSubject;
    email.Body = emailBody;
    try{
    email.Send();
    } catch(Exception e) { return; }
    onSuccess();
}

如果你的方法必须是 void return 类型(这是我根据问题标题假设的),那么你可以引入一个 out 参数。它不必在方法调用之前初始化,但您必须在方法本身内部为其分配一个值。 out 参数通过引用传递,因此被调用方为其分配的任何值都会反映在调用方中。

bool sentSuccessfully;
SendEmail(string emailBody, string emailSubject, out sentSuccessfully)

if(sentSuccessfully)
{
    // do whatever you need
}

当然,您需要修改 SendEmail 方法以包含此附加参数。

如果知道邮件发送不成功会抛出异常,可以这样处理:

try
{
    SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine);
    BOAssistant.WriteLine  //log success here
}
catch(Exception ex)
{
    //unsuccessful here
}    

最好的选择是将 return 类型的 sendmail 方法更改为 bool 并在 if 语句中使用它。

private static bool SendEmail(string emailBody, string emailSubject)
 {
   try
   {
    Email email = new Email();
    email.To.Add("POvermyer@TandT.com");
    email.Subject = emailSubject;
    email.Body = emailBody;
    email.Send();
    return true;
   }
   catch(Exception ex)
   {
    return false; 
   }
}

如果无法更改方法 return 类型,那么您可以这样做,

bool IsSuccessfullySent;
try
{
 SendEmail(BuildEmailBody(transaction, myHomeInformation),subjectLine);
 IsSuccessfullySent= true;
}
catch(Exception ex)
{
 IsSuccessfullySent=false;
}
if (IsSuccessfullySent)
 {
   BOAssistant.WriteLine                 
 }