ASP.NET BasePage back referencing to concrete implementation

孤街浪徒 提交于 2019-12-07 16:25:27

I don't see how (TView)this is expected to work. this is referring to the class which happens to be a Page. You can't convert a Page to an IView.

Your current implementation doesn't look at all hacky.

Am I missing something?

EDIT: now that I understand your situation a little better; what about having ViewBasePage inherit from IView (And removing it from your _Default page)?

EDIT Furthermore, if you then want the _Default page to have to implement the functions defined in the Interface, you can have the ViewBasePage class implement the interface's functions abstractly.

public class _Default : ViewBasePage<Presenter<IView>, IView>
{
    #region Overrides of classB
    public override void test()
    {
        //perform the test steps.
    }
    #endregion
}
public abstract class ViewBasePage<TPresenter, TView> :
    Page, IView
    where TPresenter : Presenter<TView>
    where TView : IView
{
    protected TPresenter _presenter;

    public TPresenter Presenter
    {
        set
        {
            _presenter = value;
            _presenter.View = (TView)((IView)this); //<- Now it does work
        }
    }
    #region Implementation of IView
    public abstract void test();
    #endregion
}
public interface IView
{
    void test();
}
public abstract class Presenter<TView> where TView : IView
{
    public TView View { get; set; }
    public virtual void OnViewInitialized(){}
    public virtual void OnViewLoaded(){}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!