Reputation: 101
I'm actually trying to make a Base MVP framework for my system. I have IPresenter
, IView
, PresenterBase<IView>
and ViewBase<IPresenter>
.
Here is the code:
public interface IPresenter
{
IAppController GetAppController();
}
public interface IView
{
void AttachPresenter<T>(T presenter) where T : IPresenter;
}
public abstract class BasePresenter<TView> : IPresenter where TView : class, IView
{
protected readonly TView View;
protected readonly IAppController AppController;
protected BasePresenter(TView view, IAppController appController)
{
View = view;
AppController = appController;
View.AttachPresenter(this);
}
public IAppController GetAppController()
{
return AppController;
}
}
public class BaseView<TPresenter> : Form, IView where TPresenter : class, IPresenter
{
protected TPresenter Presenter;
public void AttachPresenter<T>(T presenter) where T : IPresenter
{
Presenter = presenter; // <-- ERROR HERE
}
}
When I try to set the Presenter with AttachPresenter
method, I get this error:
" Cannot implicitly convert type 'T' to 'TPresenter' "
Why? Both are implementing IPresenter
. How can I solve it? I want this generic classes to implement every view and presenter in the system.
Sorry for bad english, I hope you understand.
Upvotes: 0
Views: 68
Reputation: 55
although T
and TPresenter
are implementing IPresenter
, but the complier don't think there are same object, for example: you have an IAnimal
, and both Cat
and Dog
implement IAnimal
, but you can NOT assign a Cat
to Dog
like this dog = cat
, i think that's the reason.
so, why not just change your function to
public void AttachPresenter(TPresenter presenter)
{
Presenter = presenter;
}
Upvotes: 1
Reputation: 4833
Since you need same type of presenter you can just use:
public void AttachPresenter(TPresenter presenter)
{
Presenter = presenter;
}
Upvotes: 1