Are there any ways to inject functionality into methods? Say you have a class that inherits from an abstract class, is there any way the abstract class can do something at the start of every method call, without the abstract class already knowing about all the methods, and without the inherited class having to call something on the abstract class at the start of every method?
Maybe it's easier to explain with an example. Let's say I have these classes:
public abstract class Foo {
SomeMagicStuff() {
Console.WriteLine("Hello from Foo");
}
}
public class Bar : Foo {
public void Hello() {
Console.WriteLine("Hello from Bar");
}
public void Hello2() {
Console.WriteLine("Another hello from Bar");
}
public static void Main() {
Hello();
Hello2();
}
}
Is there any way to have Foo inject some functionality into all methods of Bar, even though Foo has no knowledge of all the methods of Bar? For this example I would then want to output to be:
Hello from Foo
Hello from Bar
Hello from Foo
Another hello from Bar
Is this even possible?