本文介绍了访问被“新”隐藏的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好, 这个例子: 公共类BaseC { public int x; public void Invoke(){} } 公共类DerivedC:BaseC { new public void Invoke(){} } 来自这里: http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx 我可以使用BaseC.Invoke()访问原始(隐藏)方法Invoke()。 现在看看: 公共类BaseC { public int x; public void Invoke(){} } 公共类DerivedC:BaseC { public int y; } 公共类DerivedDerivedC:DerivedC { public int z; new public void Invoke(){} } 如何访问原始文件(h idden)方法? BaseC.Invoke()不起作用?为什么? 问候,罗伯特 Hello, This example: public class BaseC{public int x;public void Invoke() {}}public class DerivedC : BaseC{new public void Invoke() {}} is from here: http://msdn2.microsoft.com/en-us/library/435f1dw2.aspxI can access the original (hidden) method Invoke() by using BaseC.Invoke(). Now look at this: public class BaseC{public int x;public void Invoke() {}} public class DerivedC : BaseC{public int y;} public class DerivedDerivedC : DerivedC{public int z;new public void Invoke() {}} How do I access the original (hidden) method ? BaseC.Invoke() doesn''t work? Why? regards, Robert 推荐答案 你的意思是base.Invoke(),来自 例子的实例方法? " BaseC.Invoke()"将暗示静态方法,并且Invoke()表示静态方法。是 在这种情况下不是静态方法。 Do you mean "base.Invoke()", as from within an instance method forexample? "BaseC.Invoke()" would imply a static method, and "Invoke()" isnot in this case a static method. 再次,你的意思是base.Invoke()? 如果是这样的话,你可以'因为直接基类没有这样的 方法。但是,你可以将你的实例转换为BaseC并以这种方式访问​​ 方法。 也就是说,隐藏一个几乎总是错误的无论如何,这是一个类成员。 你很容易让代码无法正常运行或按照预期运行,因为基类没有必要有什么方法可以知道 关于或使用隐藏的方法。 Pete Again, do you mean "base.Invoke()"? If so, then you can''t because the immediate base class has no suchmethod. However, you can cast your instance to BaseC and access themethod that way. That said, it''s almost always a mistake to hide a class member anyway.It''s very easy for code to fail to operate correctly or as expected whenyou do so, because the base classes won''t necessarily have any way to knowabout or use the hidden method. Pete 我也考虑过铸造可以解决问题: ((BaseClass)this).MyMethod(); 但是我有另一个问题:MyMethod()受到保护,我不想再改变它。 所以我得到了错误消息: 无法通过类型为''type1''的限定符访问受保护成员''member''; 限定符必须是''type2类型''(或从中衍生出来)" 有什么想法吗? 问候,罗伯特 I also thought about casting would do the trick:((BaseClass)this).MyMethod();But I have another problem: MyMethod() is protected, and I don''t want tochange that. So I get the error message:"Cannot access protected member ''member'' via a qualifier of type ''type1'';the qualifier must be of type ''type2'' (or derived from it)" Any idea? regards, Robert 再次,你的意思是base.Invoke()? 如果是这样的话,你可以'因为直接基类没有这样的 方法。 Again, do you mean "base.Invoke()"?If so, then you can''t because the immediate base class has no suchmethod. 是的,从BaseC正确继承,base.Invoke()只需要工作。 工作。 b $ b - J. http ://symbolsprose.blogspot.com 这篇关于访问被“新”隐藏的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 11:11