What I want to do is to execute a method on one of various objects, but before and after the method call, I need to execute functions to do various maintenance tasks. The functions to run before and after are always the same. The method's return type and parameters can be almost anything.
Currently, I'm doing this by defining delegates for every possible method signature that I'm using, and it's getting stupendously awkward to do. This is one example:
protected delegate void DelVoidString(string string1);
protected void Execute(DelVoidString p, string string1)
{
PreInvoke();
p.Invoke(string1);
PostInvoke();
}
Other than the signatures, the code on all of them is identical. So my question is: is there a better way of doing this? I'm not attached to using delegates, just as long as I have some means of executing PreInvoke() and PostInvoke() before and after those method calls that need them, without having to remember to write out that code every single time.
I have only a vague knowledge of Lambda expressions...can they help me out better here? Or am I missing something else stupendously obvious? Thanks!