Update
unfortuantely that doesnt help in my case, because I neither control
other parts of code, neither main() like executions... I am just
writing a module for existing app, which only uses my Parent_1 as
intermediate between Parent_2 and Base
You can't and there is no way to do this I know (not even with reflection), Polymorphism in C# actually guarantees that the overridden method will be called unless you tell it to do so in the overridden method with base
Original
If I understand what you are describing, overriding won't fire the base method unless you explicitly tell it to do so.
public class Parent_2 : Parent_1
{
public override void hello ()
{
base.hello();
Print("Hi 2");
}
}
Additional Resources
Polymorphism (C# Programming Guide)
Base classes may define and implement virtual methods, and derived
classes can override them, which means they provide their own
definition and implementation. At run-time, when client code calls the
method, the CLR looks up the run-time type of the object, and
invokes that override of the virtual method. Thus in your source code
you can call a method on a base class, and cause a derived class's
version of the method to be executed.
base (C# Reference)
The base keyword is used to access members of the base class from
within a derived class:
public class Base
{
public virtual void hello()
{
}
}
//
public class Parent_1 : Base
{
public override void hello()
{
Console.WriteLine("Hi 1");
}
}
public class Parent_2 : Parent_1
{
public override void hello()
{
base.hello();
Console.WriteLine("Hi 2");
}
}
public static void Main()
{
Parent_1 p = new Parent_2();
p.hello();
}
Output
Hi 1
Hi 2
Full Demo Here
base.Hello();
inParent_2
'shello
method.ConvertFromParent_2ToParent_1
function, which you can use to create a newParent_1
from an instance ofParent_2
and then invoke the function on that new instance. It is hard to know whether that is useful to your specific context.hello
method changes something, that you have access to, you can somehow check if changes were made, callhello
fromParent_1
, but if it is as you have described, you can't without accessing (adding some code) classParent_2
.Parent_2
's hello is called when a base property namedxyz
changes.