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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:14:03+00:00 2026-05-24T06:14:03+00:00

How would one best go about allowing the input of a c# program to

  • 0

How would one best go about allowing the input of a c# program to control method invocation? For example:

Assume we have a delegate type:

delegate void WriteMe(); 

And a couple of methods:

void PrintInt() { Console.WriteLine(10); }
void PrintString() { Console.WriteLine("Hello world."); }

And allowing the input to select the invocation order:

public static WriteMe ProcessInvocationInput(int[] val) {
    WriteMe d = null; 
    foreach (int i in val) {
        switch (i) {
            case 1: d += PrintInt; break; 
            case 2: d += PrintString; break; 
        } 
    }
}

And the code that calls it all:

static void Main(string args[]) {
    int[] values = {1, 2, 3}; // Obviously this array could be filled 
                              // from actual input (args, file, wherever)
    WriteMe d = ProcessInvocationInput(values); 

    d(); 
}

The reason I’m posting this question is because it seems rather complex to implement what seems like a simple idea. I know another way to accomplish this behavior is with the reflection API, but that would be even more convoluted.

  • 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-24T06:14:03+00:00Added an answer on May 24, 2026 at 6:14 am

    That really depends on the scope you’re trying to cover. For simple cases you could use a switch (I’d suggest and enum to make it clear):

    enum InputCommands
    {
        PrintHelloWorld = 1,
        ExecuteFixedBinary = 2,
        ...
    }
    
    switch((InputCommands)inputInt)
    {
        case InputCommands.PrintHelloWorld: ...
        case InputCommands.ExecuteFixedBinary: ...
    }
    

    But if you’re writing a shell, than you need something more robust, like some sort of IExecutableCommand interface implemented by various classes.

    interface IExecutableCommand
    {
        void Execute(string arg);
    }
    

    You will have to implement some parser to handle multiple invocation requests and/or handle more complex arguments.

    If you want to use Reflection, be sure to validate your input! That could be done by only executing methods with a custom attribute on them.

    class ExecutableMethodAttribute : Attribute { }
    
    [ExecutableMethod]
    void Foo() 
    { 
       ...
    }
    

    Filtering out methods with this attribute is easy enough:

    someAssembly.GetTypes()
      .SelectMany(t => t.GetMethods())
      .Where(mi => mi.GetCustomAttributes(typeof(ExecutableMethodAttribute), true).Any())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read few good articles about NHibernate localization but nothing seems to fit
I have made some c code for a program, which does some psycho-acoustics on
Normally one would build a related model instance through its parent object: @child =
I am currently in the planning stages for a fairly comprehensive rewrite of one
for a client, I have been developing an app which has been tailored to
I'm trying to solve a problem where I have some classes in which I
I have a C++ application designed according to a classic Model-View-Controller pattern . The
I just read through this article on named function expressions and their incompatibilities with
Well, this architectural issue has been wandering in my mind for a while. Suppose
I'm doing a project for a surveying company in Drupal 6. Ultimately, employees will

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.