如何为 Page 和 UserControl 创建基类?
Page 和 UserControl 的基类:
public class MyWebPage : System.Web.UI.Page { }
public class MyUserControl : System.Web.UI.UserControl { }
它们中的任何一个都可能使用的帮助程序:
void SetSessionValue<T>(string key, T value) { Session[key] = value; }
如何实现类似以下内容?
public class WebObject // can't inherit from both Page and UserControl {
protected void SetSessionValue<T>(string key, T value) {
Session[key] = value;
}
}
public class MyWebPage : WebObject { }
public class MyUserControl : WebObject { }
更新:我兴奋了一秒钟,希望我能用这种方式解决它,但遗憾的是它无法编译。
public class WebObject<T> : T
{
}
public class MyWebPage : WebObject<System.Web.UI.Page>
{
}
Base classes for Page and UserControl:
public class MyWebPage : System.Web.UI.Page { }
public class MyUserControl : System.Web.UI.UserControl { }
Helper that either of them might use:
void SetSessionValue<T>(string key, T value) { Session[key] = value; }
How can I achieve something like the following?
public class WebObject // can't inherit from both Page and UserControl {
protected void SetSessionValue<T>(string key, T value) {
Session[key] = value;
}
}
public class MyWebPage : WebObject { }
public class MyUserControl : WebObject { }
Update: I got excited for a second hoping I could solve it this way, but alas it doesn't compile.
public class WebObject<T> : T
{
}
public class MyWebPage : WebObject<System.Web.UI.Page>
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不能。反正不容易。我建议只为页面和用户控件创建一个基类,并在两者中复制通用代码。由于用户控件包含在页面中,因此您还可以将基本用户控件类中的方法委托给基本页面类,只需将
Page
属性强制转换为您自己的类型:您还可以通过以下方式让您的生活变得悲惨:创建由两个基类实现的接口,并使用 DI 拦截方法和属性访问器调用,然后将其路由到实际提供实现的某种公共代理类。我可能不会去那里:)
You can't. Not easily anyway. I'd recommend just creating a base class for pages and user controls, and duplicating the common code in both. Since user controls are contained in pages, you can also delegate methods in the base user control class to the base page class simply by casting the
Page
property to your own type:You can also make your life miserable by creating an interface implemented by both base classes and using DI to intercept method and property accessor calls, which would then be routed to some kind of common surrogate class that actually provides the implementation. I probably wouldn't go there :)
IIRC Page 和 UserControl 均继承自 TemplateControl,这样您就可以继承它。
IIRC both Page and UserControl inherit from TemplateControl so you might be able to inherit from that.
避免重复的一种方法是使用一个帮助器类,通过静态属性实例化并可以从 UI 中的任何位置(Page、UserControl 或 UI 层中的任何其他类)访问。
类似于:
单个 ApplicationContext 实例将在当前请求的生命周期内有效,并且您可以在 UI 层代码中的任何位置使用
ApplicationContext.Current.SetSessionValue
和其他常见成员。我经常比将
SetSessionValue
这样的通用方法放在这样的帮助器类中更进一步,并且可能在其中具有应用程序特定的属性,例如通过这种方式,您可以访问当前的
ShoppingBasket
code> 实例在 UI 中的任何位置,而不知道或关心它是否缓存在 Session 中 - 这是只有您的ApplicationContext
类知道的实现细节。One way to avoid duplication is to have a helper class, instantiated through a static property and accessible from anywhere in your UI (Page, UserControl or any other classes in the UI tier).
Something like:
A single ApplicationContext instance will live for the lifetime of the current request, and you can use
ApplicationContext.Current.SetSessionValue
and other common members from anywhere in your UI tier code.I often go further than putting general-purpose methods like your
SetSessionValue
in such a helper class, and might have application specific properties in there, e.g.In this way you can access the current
ShoppingBasket
instance anywhere in your UI without knowing or caring that it's cached in Session - that's an implementation detail known only to yourApplicationContext
class.在 Pre_Init 和 Load 覆盖中调用静态函数
这是最简单的方法。
call your static functions in the Pre_Init and Load overrides
That's the easiest way.