How to listen to an abstract method?

In my abstract class, can I listen to an abstract method and fire an event whenever this method is called? If so, how?

+5
source share
3 answers

Not like an abstract method is always overridden, and there is no guarantee that the call will be redefined base.Method()to its implementation.

It is best to create a virtual method that raises the event and then make all your overrides a call base.Method()

If you want to intercept a method call, here is the question of how to do this.

+2
source

, (IoC) , , .

Unity Spring. .

+3

The best way to do this is:

public abstract class MyClass {

    public void DoOuter() {
        FireEvent();
        DoInner();
    }

    protected abstract void DoInner();
}

When someone wants to call doInner, they need to call DoOuter()to execute it. To specify functionality, you override DoInner(). This way, FireEvent()it is always called before the functionality is specified DoInner()... unless it is called directly by a child class that you cannot protect.

+3
source

All Articles