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 8494025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:11:11+00:00 2026-06-10T23:11:11+00:00

I don’t like switch statement. However, I cannot produce any alternative idea to situation

  • 0

I don’t like switch statement. However, I cannot produce any alternative idea to situation as follow. Give me an idea please.

// Parses MyTask object and routes for execution at a new task
private void PerformTask(MyTask task)
{
    Action<object> taskAction = null;

    // routing
    switch (task.Command)
    {
        case "MethodX":
            taskAction = MethodX;
            break;

        case "MethodY":
            taskAction = MethodY;
            break;

        default:
            break;
    }

   if (taskAction == null)
        return;

    Task t1 = Task.Factory.StartNew(taskAction, task);
}

private void MethodX(MyTask task)
{
    // do it something
}

private void MethodY(MyTask task)
{
    // do it something different
}

// task holder struct
public struct MyTask
{
    public string Command;
    public int[] Args;
    public int Id;

    public MyTask(string command, int[] args, int id)
    {
        this.Command = command;
        this.Args = args;
        this.Id = id;
    }

}

// Gets next task as a MyTask object and performs it.
// Thread method that is polls task queue
private void DoSomeWorks()
{
   ...
   MyTask task = GetNextTask();
   PerformTask(task);
   ...
}

I have some different tasks that should run asynchronously. These are held as MyTask struct.

In DoSomeWorks method, the next task is taken from my task queue and then passed to PerformTask method for execution. PerformTask method (nitty-gritty) determines that which method will be processed for the task. I just implemented this routing operation with switch case statement. But, I want to do this dynamically. How can I invoke the methods dynamically with command string?

I tried something like this;

MethodInfo methodInfo = typeof(MyClass).GetMethod(task.Command);

but I cannot convert from MethodInfo to Action. Well, I need help.

EDIT :

Thanks for your answers. I have tried both suggestions. This is what i need:

private void PerformTask(MyTask task)
{
  MethodInfo methodInfo = typeof(MyClass).GetMethod(task.Command);

  if (methodInfo != null)
    Task.Factory.StartNew(() => methodInfo.Invoke(this, new object[] { task }));
}

I don’t need Action object anymore. So, I can pass generic param object to Methods. Thank you so much

  • 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-10T23:11:13+00:00Added an answer on June 10, 2026 at 11:11 pm

    What about:

    MethodInfo methodInfo = typeof(MyClass).GetMethod(task.Command);
    if (methodInfo != null)
      Task.Factory.StartNew(() => methodInfo.Invoke(this, new []{ task }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't be afraid to use any technical jargon or low-level explanations for things, please.
Don't ask me how but I'm in a situation where I have DCPs published
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
(Don't know if this is strictly on-topic, but I don't see any better Stack
I don't know: if this works. if it's a good idea. what it is
Don't know if this is possible, but I have some code like this: val
Don't ask why, but is there any way to suppress a failed linking error?
Don't yell at me! i have seen many threads claiming that HTML cannot be
Don't know if I'm missing something here. I'd like to run multiple global variables
don't know if the title describes anything about what I'm trying to say but

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.