扩展基类
当您在 ASP.net 中创建网页时,代码库继承 System.Web.UI.Page
现在,当我扩展页面对象以检查 SessionExpired 时,例如,我创建一个新类并从 system.web.ui.page 继承,并覆盖一些函数。
那么问题是,在每个页面上,我必须用我的自定义页面类替换 system.web.ui.page 。
有没有办法通过添加方法或直接修改其方法来扩展类(例如,向 System.Web.UI.Page 添加 sessionExpired 检查)?
我正在使用.NET 2.0
When you create a webpage in ASP.net, the codebase inherits System.Web.UI.Page
Now when I extend the page object to check for SessionExpired for example, I create a new class and inherit from system.web.ui.page, and overwrite some functions.
The problem then is that on each page, I have to replace system.web.ui.page with my custompage class.
Is there a way to extend a class by adding methods or modifying its methods directly (e.g. add the sessionExpired check to System.Web.UI.Page) ?
I'm using .NET 2.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为 System.Web.UI.Page 类添加扩展方法,该方法将为您检查会话到期时间。我认为这样的事情会起作用。
扩展方法是 C# 3.0 的功能,因此您必须安装 C# 3.0 编译器来编译代码(VS.NET 2008 或更高版本)。
当您使用 .NET 2.0 时,您必须创建一个属性,因为扩展方法依赖于仅是 .NET 3.5 (System.Core.dll) 一部分的属性。只需在您的项目中创建以下属性,您就可以使用扩展方法:
You can add extension method for System.Web.UI.Page class that will check session expiry for you. I think something like this will work.
Extension method is feature of C# 3.0, so you will have to install C# 3.0 compiler to compile the code (VS.NET 2008 or higher)
As you are using .NET 2.0, you will have to create an attribute because extension method have dependency on an attribute that is only part of .NET 3.5 (System.Core.dll). Just create the following attribute in your project and you will be able to use extension methods:
VB 2008支持扩展方法,但是:扩展方法不能重写基类方法,只能添加新方法。他们无权直接访问扩展类型的成员。扩展不是继承。因此,您可以通过扩展方法添加 sessionExpired 检查并在需要时调用它,但不能使用它向 System.Web.UI.Page 的行为添加任何触发机制。
VB 2008 supports Extension Methods, but: Extension Methods can't override base class methods, only add new methods. They don't have direct access to the members of the extended type. Extending is not Inheriting. Therefore, you can add the sessionExpired check via extension methods and invoke it whenever you need, but you can't use it to add any triggering mechanism to System.Web.UI.Page's behaviour.