简体   繁体   中英

How to Intercept, skip method execution and continue with the rest of the stack

Is there a way to intercept a method call, look for a condition and skip the execution? The method doesn't return anything and I would like to consider it as successfully completed and continue the execution with the rest of the stack.

public IMessageSink NextSink {
    get { return m_next; }
}

public IMessage SyncProcessMessage(IMessage msg) {
    if (//some condition is met) {
        // should skip the method execution
    } else {
        IMessage returnMethod = m_next.SyncProcessMessage(msg);
        return returnMethod;
    }
}

The return statement terminates execution of the method. In this case you should use return null because SyncProcessMessage() have to return value of IMessage

public IMessage SyncProcessMessage(IMessage msg) {
    if (//some condition is met) {
        return null;
    } else {
        IMessage returnMethod = m_next.SyncProcessMessage(msg);
        return returnMethod;
    }
}

or something like that:

public IMessage SyncProcessMessage(IMessage msg) {
    IMessage returnMethod  = null;
    if (! //not some condition is met) {
        returnMethod = m_next.SyncProcessMessage(msg);            
    }
    return returnMethod;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM