简体   繁体   English

为什么我不能在没有强制转换的情况下访问基类的成员?

[英]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.Pageobject

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.

相关问题 如何通过Visual Studio中的“即时”窗口访问派生类的基类的成员? - How can I access members of a derived class's base class through the Immediate window in Visual Studio? 通用接口/基类-无类型约束的访问成员 - Generic interface/base class - access members without type constraints C#-内部实例方法,为什么不使用类名就可以访问静态成员? - C# - Inside instance method, why can we access static members without using the class name? 是否可以在不强制转换的情况下调用基础对象的方法? - Can I call a method of a base object without casting it? 为什么我不能从派生类的XML文档注释标记中引用基类中的可访问成员? - Why can't I reference the accessible members in a base class from within XML documentation comment tags in a derived class? 为什么我不能在T4模板中的类成员中访问实用程序方法? - Why can't I access the utility methods in class members in T4 templates? 如何访问基类的部分成员 - How to access part of the members of base class 为什么初始化派生类变量并将其分配给其基类类型不允许访问其成员? - Why does initialising a derived class variable and assigning to its base class type not allow access to its members? 如何在不使用方法或构造函数的情况下直接访问派生 class 中的基本 class 属性? - How can I directly access my base class property in the derived class without using methods or constructors? 为什么基类中的列表成员被更改? - Why are members of the list in the base class being changed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM