我有一个界面:

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没有实现IReminderIIdentifiableEntity,我会理解的,但是确实可以。

我究竟做错了什么?

最佳答案

什么是TTimeSheetReminderIReminder<InvoiceSummary>,因此如果T不是InvoiceSummary,则无法进行引用转换:

class Foo: IIdentifiableEntity { ... }
var reminder = new TimesheetReminder() as IReminder<Foo>; //returns null

08-06 02:13