S
S
stepagrus2018-03-14 08:16:06
OOP
stepagrus, 2018-03-14 08:16:06

How to call parent's parent's method?

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()
    {
      //как вызвать Method из класса A?
    }
  }

how to call Method from class A?
Important: classes A and B are library classes and we cannot change them.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
S
stepagrus, 2018-03-14
@stepagrus

This cannot be done legally.
https://docs.microsoft.com/ru-ru/dotnet/csharp/lan...
Class B stores the implementation of the base class A .
Class C stores the implementation of its base class B , but we do not have access to the implementation of A using C# language tools.
A virtual method of class A can still be called from class C using a dirty IL code hack, but these are not our methods.

#
#, 2018-03-14
@mindtester

(this as A).Method()
ps
.
_ _ _
_
_ review the architecture for the use of interfaces and extensions .. in recent years I have been implementing almost 90% of the code in extensions, very rarely new classes (more often just structures, and now more and more often tuples .. or even completely unnamed tuples ;))), and only when necessary, interfaces
about extensions are generally a tool that allows many different elegant solutions
in particular, it is possible to implement extensions of the same name for the entire hierarchy A, B, C, and according to the difference in the signature of the input parameters, they will not conflict. including no problems in inheritance and redefinition
in general, over the years, I am convinced that fencing your class hierarchy is sooo far NOT
always
justified based extensions https://dotnetfiddle.net/UTPZLv

C
cicatrix, 2018-03-14
@cicatrix

If you can only modify method C, then you need to inherit it from A. Then you can.

// Наследуем C от B
  public class C : B
  {
      // Пользуемся функционалом B, унаследованным от B
  }
  // Добавляем надкласс, наследующий от A
  public class D : A
  {
     public C myBfunctional { get; set; } // Через это свойство получаем функционал B
     public void Method()
     {
         base.Method(); // Пользуемся методом, унаследованным из A
     }
  }

A
Alexander Kuznetsov, 2018-03-14
@DarkRaven

I recommend paying attention here - https://stackoverflow.com/a/16905179/2822609
In general, why such difficulties?

L
LiptonOlolo, 2018-03-14
@LiptonOlolo

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

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

        protected void MethodInA()
        {
            base.Method();
        }
    }

    public class C : B
    {
        public void Some()
        {
            MethodInA();
        }
    }

It can be so, because protected there, then Method from class A can be called ONLY in B.

S
ssrdop, 2018-03-14
@ssrdop

To call a class A method from class B, try
base.method();
https://docs.microsoft.com/ru-ru/dotnet/csharp/lan...

R
Roman, 2018-03-15
@yarosroman

Reflection to help you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question