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

  • Home
  • SEARCH
  • 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 5995707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:59:54+00:00 2026-05-22T23:59:54+00:00

What do I have to do to say that InvokeMethod can invoke a method

  • 0

What do I have to do to say that InvokeMethod can invoke a method and when using special options like Repeat it shall exexute after the Repeat.

My problem for now is that the method will already exexute before it knows that it has to be called 100 times.

class Program
{
    static void Main()
    {
        const bool shouldRun = true;

        new MethodExecuter()
            .ForAllInvocationsUseCondition(!Context.WannaShutDown)
                .InvokeMethod(A.Process).Repeat(100)
                .When(shouldRun).ThenInvokeMethod(B.Process).Repeat(10)
            .ForAllInvocationsUseCondition(Context.WannaShutDown)
                .When(shouldRun).ThenInvokeMethod(C.Process);
    }
}

MethodExpression

public class MethodExpression
{
    private bool _isTrue = true;
    private readonly MethodExecuter _methodExecuter;
    public MethodExpression(bool isTrue, MethodExecuter methodExecuter)
    {
        _isTrue = isTrue;
        _methodExecuter = methodExecuter;
    }

    public MethodExecuter ThenInvokeMethod(Action action)
    {
        if (_isTrue)
        {
            action.Invoke();
            _isTrue = false;
        }
        return _methodExecuter;
    }
}

MethodExecuter

public class MethodExecuter
{
    private bool _condition;
    private int _repeat = 1;

    public MethodExpression When(bool isTrue)
    {
        return new MethodExpression(isTrue && _condition, this);
    }

    public MethodExecuter InvokeMethod(Action action)
    {
        if (_condition)
        {
            for (int i = 1; i <= _repeat; i++)
            {
                action.Invoke();
            }
        }
        return this;
    }

    public MethodExecuter ForAllInvocationsUseCondition(bool condition)
    {
        _condition = condition;
        return this;
    }

    public MethodExecuter Repeat(int repeat)
    {
        _repeat = repeat;
        return this;
    }
}
  • 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-05-22T23:59:55+00:00Added an answer on May 22, 2026 at 11:59 pm

    There are a lot of ways to skin this cat, but I think one source of this difficulty is in the fact that you actually invoke the method within the InvokeMethod() method (go figure!).

    Typically, we use fluent APIs to turn syntax that is evaluated from the inside-out into something that can be expressed in a left-to-right fashion. Thus, the expression builder components of the interface are used to build up state throughout the expression, and only at the end does the “real work” happen.

    One solution to your immediate problem is to queue up each action with its associated options (invocation conditions, repeat count, etc.), and add some ExecuteAll() method to MethodExecuter that dequeues and executes the fully configured actions at the end of the member chain.

    Another solution would be to put all of the execution options inside the InvokeMethod() method; something like:

    .Invoke(x => x.Method(A.Process).Repeat(100))
    

    This method would look something like:

    public MethodExecuter Invoke(Action<IExecutionBuilder> executionBuilder)
     {
         var builder = new ExecutionBuilder();
          executionBuilder(builder);
    
          var action = builder.Action; 
          var repeat = builder.RepeatCount;
    
          if (_condition)
          {
              for (int i = 1; i <= repeat; i++)
                {
                    action();
                }
          }
    
          return this;
     }
    

    I haven’t worked through this in Visual Studio, but the other items would be something like:

    public interface IExecutionBuilder
     {
         IExecutionBuilder Method(Action action);
          IExecutionBuilder Repeat(int count);
     }
    
     public class ExecutionBuilder : IExecutionBuilder
     {
          public ExecutionBuilder()
          {
              RepeatCount = 1; // default to repeat once
                Action = () => {}; // default to do nothing, but not null
          }
    
         public IExecutionBuilder Method(Action action)
          {
              Action = action;
              return this;
          }
    
          public IExecutionBuilder Repeat(int repeat)
          {
              RepeatCount = repeat;
                      return this;
          }
    
          public int RepeatCount { get; private set; }
          public Action Action { get; private set; }
     }
    

    Note that RepeatCount and Action are not exposed on the interface. This way, you will not see these members when calling .Invoke(x => x., but will have access to them when using the concrete ExecutionBuilder class inside the Invoke() method.

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

Sidebar

Related Questions

Say that I have two models- Users and Accounts. Each account can have at
Say that I have two tables like those: Employers (id, name, .... , deptId).
I'm a newcomer to PDO and have to say that I like it so
So for starters lets say that I have a LinkedList<String> , I can easily
Say I have a method like this: public void SaveData() { try { foreach
at work we have been using JGroups and I have to say that I
I have seen people say that it is bad form to use catch with
Lets say that you have websites www.xyz.com and www.abc.com. Lets say that a user
Let say that I have a website with some information that could be access
Lets say that I have a header user control in a master page, and

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.