发现无效记录时,如何安全地 return 一个空值?

How can I safely return a null when an invalid record is found?

我有这个代码:

private static QueuedReports GetSingleReportForUnit(string unit, int RptID, DateTime nextExDate)
{
    ReportSchedule rs = ReportSchedulerSQL.GetReportSchedulerRecord(unit, RptID);
    DateTime d8 = new DateTime(0001, 1, 1); //DateTime.Now.AddYears(1);
    if (rs.NextExecution.Date == d8.Date)
    {
        ;// return null; <= I get, "cannot access a closed stream" with this
    }
    QueuedReports qr = new QueuedReports();
    qr.Unit = unit;
    qr.ReportName = GetReportNameForID(RptID);
    List<String> emailAddresses = ReportSchedulerSQL.GetEmailAddressesForUnitRpt(unit, RptID);
    qr.AllEmailAddresses = string.Join(",", emailAddresses.ToArray());
    qr.NextExecution = nextExDate; 
    qr.NextExecutionsBeginDateArg = GetNextExecutionsBeginDateArg(unit, RptID, nextExDate);
    qr.NextExecutionsEndDateArg = GetNextExecutionsEndDateArg(unit, RptID, nextExDate);
    return qr;
}

...从这里调用:

private static IEnumerable<QueuedReports> GetAllFutureReportsForUnit(string unit, int RptID, DateTime finalDate)
{
    List<QueuedReports> listToReturn = new List<QueuedReports>();
    DateTime currentDate = DateTime.Now;
    while (currentDate <= finalDate)
    {
        currentDate = ReportSchedulerConstsAndUtils.GetNextDateForUnitReportAfter(unit, RptID, currentDate);
        var qr = GetSingleReportForUnit(unit, RptID, currentDate);
        listToReturn.Add(qr);
    }
    return listToReturn;
}

如果在GetSingleReportForUnit()中找到一条有效的记录,也就是说"NextExecution"不是“0001,1,1”的值,则一切正常;但是,如果没有,我尝试 return null 编译,但在运行时失败 "cannot access a closed stream"

当无效日期(第一年的 1 月 1 日)位于 rs.NextExecution 时,如何短路执行 GetSingleReportForUnit()?

QueuedReports 是一个自定义 class:

public class QueuedReports
{
    public string Unit { get; set; }
    public string ReportName { get; set; }
    public DateTime NextExecution { get; set; }
    public string AllEmailAddresses { get; set; }
    public DateTime NextExecutionsBeginDateArg { get; set; }
    public DateTime NextExecutionsEndDateArg { get; set; }
}

为什么 returning null 的行为会尝试访问流? AFAICT,此代码中没有任何流,所以什么?

我想你在找 continue :

private static IEnumerable<QueuedReports> GetAllFutureReportsForUnit(string unit, int RptID, DateTime finalDate)
{
    List<QueuedReports> listToReturn = new List<QueuedReports>();
    DateTime currentDate = DateTime.Now;
    while (currentDate <= finalDate)
    {
        currentDate = ReportSchedulerConstsAndUtils.GetNextDateForUnitReportAfter(unit, RptID, currentDate);
        if (currentDate == DateTime.MinValue)
            continue;
        var qr = GetSingleReportForUnit(unit, RptID, currentDate);
        listToReturn.Add(qr);
    }
    return listToReturn;
}

如果您的 currentDate 是 1 年的 1 月 1 日,那么 continue 会将控制权传递给 while 循环的下一次迭代。

这完全取决于 rs.NextExecution 在做什么以及它是如何工作的。如果在那里打开某种流并且它不能 return 上面有日期 属性 的对象,那么你的 if 语句将不起作用。

您可以使用 try catch 块来捕获抛出的任何异常,并在 catch 语句块中使用 return null。您可以检查该 NextExecution 对象中的流是否仍然有效,而不是检查日期 属性(如果可用)。