开发者

Why can I not pass this interface as an argument?

开发者 https://www.devze.com 2023-01-08 09:25 出处:网络
I don\'t have much experience with generics, but can someone please explain to me why this doesn\'t work and what I need to do to get it to work?

I don't have much experience with generics, but can someone please explain to me why this doesn't work and what I need to do to get it to work?

I have 3 interfaces.

public interface IOnlineView

public interface ICalendarView : IOnlineView

public interface IDateView : ICalendarView

Then I have 3 presenter classes

public abstract class OnlinePresenter<T> where T : IOnlineView
{
   protected OnlinePresenter(T view) { }
}

public class CalendarPresenter<T> : OnlinePresenter<T> where 开发者_如何学CT : ICalendarView
{
    public CalendarPresenter(ICalendarView view) : base(view) { }
}    

public class DatePresenter<T> : CalendarPresenter<IDateView>
{
    public DatePresenter(IDateView view) : base(view) { }
}    

The base(view) call on the CalendarPresenter class says that ICalendarView is not assignable to type T

Passing the IDateView to the CalendarPresenter works fine, but passing ICalendarView to the OnlinePresenter does not. Am I missing something? If ICalendarView inherits from IOnlineView, why can't it be passed as an argument?

This worked fine before I added the ICalendarView and CalendarPresenter between the Date and Online classes, but I need it so that the CalendarPresenter can handle the calendar display and can be inherited from other presenters.


Make the following change:

public class CalendarPresenter<T> : OnlinePresenter<T> where T : ICalendarView
{
    public CalendarPresenter(T view) : base(view) { }
}  

The base class 'OnlinePresenter' constructor takes an object of type T and the sub class 'CalendarPresenter' constructor took an object of type 'ICalendarView'.

0

精彩评论

暂无评论...
验证码 换一张
取 消