本文介绍了覆盖重写方法(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C#中覆盖重写的方法(如果这有意义!)。

I'm trying to override an overridden method (if that makes sense!) in C#.

我有一个类似于下面的场景,但是当我在C类的SampleMethod()中有一个断点时它没有被击中,而同一个断点在B方法正在受到打击。

I have a scenario similar to the below, but when I have a breakpoint in the SampleMethod() in the "C" class it's not being hit, whilst the same breakpoint in the "B" method is being hit.

public class A
{
      protected virtual void SampleMethod() {}
}

public class B : A 
{
      protected override void SampleMethod()
      {
           base.SampleMethod(); 
      }
}

public class C : B
{
      protected override void SampleMethod() 
      {
           base.SampleMethod(); 
      }
}

提前致谢!

修改:

好的,上下文有帮助:

这是在复合控件的上下文中,因此类A继承CompositeControl并在覆盖CreateChildControls()方法后调用SampleMethod()。

This is in the context of a composite control so class A inherits from CompositeControl and calls SampleMethod() after overriding the CreateChildControls() method.

推荐答案

在没有看到调用SampleMethod的代码的情况下,我的猜测是你有一个B类型的对象并在其上调用SampleMethod 。

Without seeing the code that calls SampleMethod, my guess would be that you have an object of type B and call SampleMethod on that.

这篇关于覆盖重写方法(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 16:30