开发者

How to listen an abstract method?

In my abstract class can I listen an abstract method and开发者_C百科 fire an event whenever that method is called? If yes how?


Abstract or no, you're looking for an Inversion of Control (IoC) framework here, specifically one that lets you do method interception.

I'd look at Unity, or Spring. There are a few others out there.


The best way to do this is as follows:

public abstract class MyClass {

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

    protected abstract void DoInner();
}

When someone wants to call doInner they have to call DoOuter() in order to execute it. To specify functionality you override DoInner(). So FireEvent() is always called before whatever DoInner() functionality is specified... unless it gets called directly by a child class, which you can't really guard against.


Not really as an abstract method is always overidden and there is no guarantee that the override call base.Method() to an implementation of it.

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜