我有两个对象:
public enum BugReportStatus { OpenUnassigned = 0,OpenAssigned = 1,ClosedAsResolved = 2,ClosedAsRejected = 3 }
和
public enum BugReportFilter { Open = 1,... Closed = 4,}
而且我想创建一个开关案例,根据BugReportFilter选择我的输出将是一个特定的BugReportStaus.
所以我创建了一个方法CheckFilter
private BugReportStatus Checkfilter(BugReportFilter filter) { switch (filter) { case BugReportFilter.Open: return BugReportStatus.OpenAssigned; case BugReportFilter.Closed: return BugReportStatus.ClosedAsResolved; } };
问题是,在BugReportFilter.Open选项的情况下,我应该返回BugReportStatus.OpenAssigned和BugReportStatus.OpenUnassigned,有没有办法在单个返回中连接这两个选项?
解决方法
你可以返回IEnumerable< BugReportStatus>:
private IEnumerable<BugReportStatus> Checkfilter(BugReportFilter filter) { switch (filter) { case BugReportFilter.Open: return new[]{ BugReportStatus.OpenAssigned,BugReportStatus.OpenUnassigned }; case BugReportFilter.Closed: return new[]{ BugReportStatus.ClosedAsResolved }; } };
然后你可以使用Enumerable.Contains检查它是否为f.e. BugReportStatus.OpenAssigned:
bool isOpenAssigned = Checkfilter(someFiler).Contains(BugReportStatus.OpenAssigned);