Answer the question
In order to leave comments, you need to log in
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?
}
}
Answer the question
In order to leave comments, you need to log in
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.
(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
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
}
}
I recommend paying attention here - https://stackoverflow.com/a/16905179/2822609
In general, why such difficulties?
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();
}
}
To call a class A method from class B, try
base.method();
https://docs.microsoft.com/ru-ru/dotnet/csharp/lan...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question