Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7837399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:37:23+00:00 2026-06-02T14:37:23+00:00

What I want to do is to execute a method on one of various

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:37:37+00:00Added an answer on June 2, 2026 at 2:37 pm

    You can use generics and the built-in System.Action delegate types to reduce some of the duplication:

    protected void Execute(Action action)
    {
        PreInvoke();
        action();
        PostInvoke();
    }
    
    protected void Execute<T>(Action<T> action, T arg)
    {
        PreInvoke();
        action(arg);
        PostInvoke();
    }
    
    protected void Execute<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2)
    {
        PreInvoke();
        action(arg1, arg2);
        PostInvoke();
    }
    
    // Additional methods for three arguments, four arguments, etc.
    

    To handle return values, use System.Func:

    protected TResult Execute<TResult>(Func<TResult> func)
    {
        PreInvoke();
        TResult result = func();
        PostInvoke();
        return result;
    }
    
    protected TResult Execute<T, TResult>(Func<T, TResult> func, T arg)
    {
        PreInvoke();
        TResult result = func(arg);
        PostInvoke();
        return result;
    }
    

    You might also want to look into aspect-oriented programming (AOP) for C#.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want my timer to execute the actionPerformed method only one time once it
I'm testing a page with Selenium IDE and want to execute a method already
I want to execute one mysql statement where I have a select that is
I want to execute a method with a copy of the original self passed
I want to see what queries are executed on django's model .save() method. Since
I have asp.net generated html code. I want execute jQuery click function when user
I want to execute an application (executable ending with an .exe extension ) that
I want to execute a program which takes in 1 text file(say like Text.exe),
I want to execute a code helloword.cpp which takes in some argument from console
I want to execute a program in memory on Windows. I do not want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.