我有一个界面:
public interface IReminder<T> where T : class, IIdentifiableEntity
{
IEnumerable<T> GetRemindersToBeSent(IRepository<T> repository);
}
和实现此接口的类
TimesheetReminder
:public class TimesheetReminder : IReminder<InvoiceSummary>
{
public IEnumerable<InvoiceSummary> GetRemindersToBeSent(IRepository<InvoiceSummary> invoiceSummaryRepository)
{
var today = DateTime.Today;
return invoiceSummaryRepository.List.Where(inv =>
inv.InvoiceSummaryStatus.CKAStatusName == "Draft" &&
inv.InsertDateUTC <= today.AddDays(-3) &&
inv.InsertDateUTC >= today.AddDays(-6) &&
inv.EndDate <= today.AddDays(-3)
);
}
InvoiceSummary
实现了IIdentifyableEntity
public static class ReminderFactory<T> where T : class, IIdentifiableEntity
{
public static IReminder<T> GetReminder(string applicationType)
{
IReminder<T> reminder;
switch (applicationType)
{
case "Invoicing":
reminder = (IReminder<T>)new TimesheetReminder();
break;
default:
reminder = null;
break;
}
return reminder;
}
}
开票的情况下返回空值。
如果
TimesheetReminder
没有实现IReminder
的IIdentifiableEntity
,我会理解的,但是确实可以。我究竟做错了什么?
最佳答案
什么是T
? TimeSheetReminder
是IReminder<InvoiceSummary>
,因此如果T
不是InvoiceSummary
,则无法进行引用转换:
class Foo: IIdentifiableEntity { ... }
var reminder = new TimesheetReminder() as IReminder<Foo>; //returns null