0

We have the following code:

  public class A
  {
    protected virtual void Method()
    {
      Console.Write("A");
    }
  }

  public class B : A
  {
    protected override void Method()
    {
      Console.Write("B");
    }
  }

  public class C : B
  {
    public void Some()
    {
      //How to call Method() from class A?
    }
  }

How to call Method() from class A in Some() method from class C?

We will assume that A and B are classes from the library and we can not change them.

Solution: https://stackoverflow.com/a/438952/8081796

Stepagrus
  • 1,189
  • 1
  • 12
  • 19
  • i don't think there's another way beside method hiding – styx Mar 14 '18 at 06:00
  • Without knowing anything about the "library" - it looks like you should be deriving from A rather than B. If the rest of the class shall behave the "B" way, you could use composition and proxy all methods but this one to an internal instance of B. ("Internal" as in "private") But really: This looks like a case for design review to me. – Fildor Mar 14 '18 at 07:43
  • Possible duplicate of [How can I call the 'base implementation' of an overridden virtual method?](https://stackoverflow.com/questions/1334254/how-can-i-call-the-base-implementation-of-an-overridden-virtual-method) – Sinatr Mar 14 '18 at 08:52

3 Answers3

2

B overrides Method() and A its marked as virtual and protected, the only way to call it (in its current format) is if B calls it somehow

public class B : A 
{
   protected override void Method()
   {
      base.Method();
      Console.Write("B");
   }
}

Or derived from A directly

public class C : A
{
   public void Some()
   {
      Method();
   }
}

virtual (C# Reference) | Microsoft Docs

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:

Furthermore

When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.

protected (C# Reference)

A protected member is accessible within its class and by derived class instances.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

If you really want Method of A to be called here (without changing implementation of A or B's Method), you have to make below changes.

  1. Change access specifier of Method of B class to new from override.

    override will override the base class method. Making it new won't do it.

  2. Change access specifier of A and B class Methods to public instead of protected

    protected members of A won't be accessible inside your C class.

With this changes, check out below code. You will see that Method from class A is getting called.

static void Main()
{
    var c = new C();
    c.Some();
    Console.ReadKey();
}
public class A
{
    public virtual void Method()
    {
        Console.Write("A");
    }
}

public class B : A
{
    public new void Method()
    {
        Console.Write("B");
    }
}

public class C : B
{
    public void Some()
    {
        //How to call Method() from class A?
        ((A)this).Method();
    }
}

If you cannot make the changes described as above, then I'm afraid you can't call A's Method :O .

Paritosh
  • 11,144
  • 5
  • 56
  • 74
0

This is impossible, because

The implementation of a virtual member can be changed by an overriding member in a derived class.

B change implementation of A, therefore C have only B implementation as base and have not implementation of A.

Solution: https://stackoverflow.com/a/438952/8081796

Stepagrus
  • 1,189
  • 1
  • 12
  • 19