简体   繁体   中英

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 .

Why do I have to cast my IHttpHandler as MyCMS.Admin.Login before I can access the members of that type and below?

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. It's a member of Page , by way of Control , I believe.

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. It has no knowledge of a Visible member since any class can implement this interface. What you ended up doing is casting to an object of a class that does inherit the Visible member.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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