简体   繁体   English

实现接口的通用基类

[英]generic base class that implements interface

How can I declare my Basepage class in an asp.net webforms project to also implement IBasePage? 如何在asp.net webforms项目中声明我的Basepage类以实现IBasePage?

public abstract class BasePage<T> : Page where T : class
{

}

and the interface 和界面

public interface IBasePage
    {
        UserProfile UserProfile { get;}
        bool IsStreamingAdmin {get;}
        int? EmplId {get;}       
    }

my ultimate goal is to be able to write code this like: 我的最终目标是能够编写如下代码:

IBasePage page = HttpContext.Current.Handler as IBasePage;
if (page.IsStreamingAdmin)
{
    //do something here....
}

你的问题对我来说并不完全清楚,但你不能这样做:

public abstract class BasePage<T> : Page, IBasePage where T : class { }
public abstract class BasePage<T> : Page, IBasePage where T : class { }

If your class implements all the methods defined in the interface your code is able to compile and you will be able to call an instance of your abstract class. 如果您的类实现了interface定义的所有方法,您的代码就可以编译,并且您将能够调用抽象类的instance

Calling page.IsStreamingAdmin will result in returning the value of the class you have an instance of. 调用page.IsStreamingAdmin将返回您拥有实例的类的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM