【问题标题】:ASP.NET BasePage back referencing to concrete implementationASP.NET BasePage 反向引用具体实现
【发布时间】:2009-09-03 14:29:39
【问题描述】:

我有一个这样设置的页面

public partial class _Default : ViewBasePage<EmployeePresenter, IEmployeeView>,
                                 IEmployeeView
{
...
}

在我的基本页面内

public abstract class ViewBasePage<TPresenter, TView> : 
        Page where TPresenter : Presenter<TView> where TView : IView
{
    protected TPresenter _presenter;

    public TPresenter Presenter
    {
        set
        {
            _presenter = value;
            _presenter.View = GetView(); // <- Works
            //_presenter.View = (TView)this; <- Doesn't work
        }
    }

    /// <summary>
    /// Gets the view. This will get the page during the ASP.NET
    /// life cycle where the physical page inherits the view
    /// </summary>
    /// <returns></returns>
    private static TView GetView()
    {
        return (TView) HttpContext.Current.Handler;
    }
}

我需要做的实际上是强制转换 (TView)_Default,使用我的 GetView() 方法确实以该结果结束。在基本页面内我做不到

_presenter.View = (TView)this;

因为这实际上是ViewBasePage&lt;TPresenter,TView&gt;,所以它不能直接转换为TView。

所以我的实际问题是,是否有任何替代方法可以让我感觉不那么 hacky,如果这是主要选择,那么通过以这种方式处理我的页面,我真的需要担心什么吗?

编辑:

我要写掉的确切部分是

private static TView GetView()
{
    return (TView) HttpContext.Current.Handler;
}

因为我觉得在这种情况下能够引用回页面是相当严重的黑客攻击。

【问题讨论】:

  • 好的,我从示例中删除了它。我把它作为一个神器留在里面,但它没有被使用。

标签: c# asp.net generics webforms page-lifecycle


【解决方案1】:

我看不出(TView)this 应该如何工作。 this 指的是恰好是 Page 的类。您无法将 Page 转换为 IView

您当前的实现看起来并不老套。

我错过了什么吗?

编辑:现在我更了解你的情况了;如何让 ViewBasePage 从 IView 继承(并将其从您的 _Default 页面中删除)?

编辑此外,如果您希望 _Default 页面必须实现接口中定义的功能,您可以让 ViewBasePage 类抽象地实现接口的功能。

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(){}
}

【讨论】:

  • 页面实现了IView,所以在Page_Load{ IView view = (IView)THIS; } 在页面级别工作正常,它是在基页面上,这指的是 BasePage,而不是它实现 IView 的页面。 hacky 部分是使用 HttpContext
  • 这只有在你能保证继承类也继承了 IView 的情况下才允许工作。要保证继承类确实继承了 IView,就意味着 ViewBasePage 也继承自 IView。
  • 我正在尝试写掉 HttpContext 调用
  • 好吧,我忘了从我的例子中删除它,现在看看。
  • 你尝试过构建这个吗?那仍然会出现铸造错误。 C# 似乎不明白 ViewBasePage 内的 TView 与 Presenter 内的 TView 相同,即使有 where 约束。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
相关资源
最近更新 更多