[英]Why can I not access members of the base class without casting?
Let's say I have a System.Web.IHttpHandler
whose base class is ASP.login_aspx
whose base class is MyCMS.Admin.Login
whose base class is... etc... all the way back to System.Web.UI.Page
and of course, object
. 假设我有一个
System.Web.IHttpHandler
其基类是ASP.login_aspx
其基类是MyCMS.Admin.Login
其基类是......等等...一直回到System.Web.UI.Page
和object
。
Why do I have to cast my IHttpHandler as MyCMS.Admin.Login
before I can access the members of that type and below? 为什么我必须
MyCMS.Admin.Login
IHttpHandler转换为MyCMS.Admin.Login
才能访问该类型及以下的成员?
Example: 例:
IHttpHandler result = base.GetHandler(context, requestType, virtualPath, path);
bool isVisible = result.Visible;//Does not work
bool isVisible = ((MyCMS.Admin.Login)(result)).Visible;//Works
//Noting that Visible is a member of System.Web.UI.Page
For clarity, I'm not expecting that result.Visible should work, I just want to know why it doesn't. 为了清楚起见,我不期待那个结果。可见的应该工作,我只是想知道它为什么没有。
Visible
is not a member of IHttpHandler
, so you should not expect to be able to call it on such a variable. Visible
不是IHttpHandler
的成员,所以你不应该期望能够在这样的变量上调用它。 It's a member of Page
, by way of Control
, I believe. 我相信它是
Page
的成员,通过Control
方式。
When you cast a variable to one of the base types/interfaces of the object's class, you can only call members that are on the type of the variable (or base classes/interfaces of that type). 当你施放一个变量以基本类型的一个/该对象的类的接口,可以只调用成员是在可变 (或基类该类型/接口)的类型。
The IHttpHandler interface is just that - an interface. IHttpHandler接口只是一个接口。 It has no knowledge of a
Visible
member since any class can implement this interface. 它不了解
Visible
成员,因为任何类都可以实现此接口。 What you ended up doing is casting to an object of a class that does inherit the Visible
member. 你最终做的是转换为继承
Visible
成员的类的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.