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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:40:28+00:00 2026-06-15T18:40:28+00:00

I have a situation where a List of strings is provided. The first entry

  • 0

I have a situation where a List of strings is provided. The first entry in the list is the name of a method. The remaining strings in the list are method arguments. I’d like to use a task to run the method (for educational purposes). I am having problems figuring out the proper procedure that would allow me to feed the method name into the Task instruction.

For this example, I have two statics methods that could be run as a Task. args[1] would
signify my selection.

public class Program
{
    private static ILog log = LogManager.GetLogger 
                    (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    static void Main(string[] args)
    {
        string whichPrint = args[1];

        Type type = typeof(Program);
        MethodInfo meth = type.GetMethod(whichPrint, BindingFlags.Public | 
                                                           BindingFlags.Static);

        //this is the problem area....how do I feed the the method or delegate into 
        //the lambda expression ????
        Delegate methDel = meth.CreateDelegate(type);
        Task t = Task.Factory.StartNew(() => methDel("help!!"));

    }

    static void printme1(string s)
    {
        log.Debug("Printme1 Printing: " + s);
    }

    static void printme2(string s)
    {
        log.Debug("Printme2 Printing: " + s);
    }
}

I can’t compile since methDel is being viewed as variable. I need it, or something else,
to be viewed as a method. I don’t want to use case/switch statements since I may have a lot of methods that could be assigned to a task.

  • 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-15T18:40:29+00:00Added an answer on June 15, 2026 at 6:40 pm

    The fact that you’re going to create a new task with this is somewhat irrelevant. All you need to do is work out how to call the method, and the rest is just “start a new task which then calls the method”.

    So you probably want something like:

    public static void CallMethod(List<string> nameAndArguments)
    {
        var method = typeof(Program).GetMethod(nameAndArguments[0]);
        method.Invoke(null, nameAndArguments.Skip(1).ToArray()):
    }
    

    Then:

    Task.Factory.StartNew(() => CallMethod(nameAndArguments));
    

    I don’t think it’s worth building a delegate unless you’re trying to get better performance out of calling that delegate lots of times.

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

Sidebar

Related Questions

I have a situation like this. I have a list of urls in a
I have a situation where I would like to present a list of the
Here is the situation: I have list which store strings which are actually numbers
See i have a situation like this... object myRoledata = List<Roles>() --> (some list
I have a situation where i want to return List<> from this function public
I have a situation where the second select list option is generated from the
I have a situation where I need to dynamically build up a list of
I have a situation in C# where I have a list of simple types.
I have this situation where I want to display a list of Administration objects
Here is my situation: I have one table that contains a list of drugs

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.