C# Debug Console creation

This is a discussion on C# Debug Console creation within the C# Programming forums, part of the General Programming Boards category; Hey all! Right now I'm working in C#, and I want it so that if a user presses '~' in ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    62

    Question C# Debug Console creation

    Hey all!
    Right now I'm working in C#, and I want it so that if a user presses '~' in my input text box, it will make the next input become a direct function.
    for example:
    Code:
    class Form1 : Form
    {
    #region unnecessary
    #endregion
        bool functions;
        private void on_textbox_keyentry(Object sender, KeyEventArgs e)
        {
             switch(e.KeyChar())
            {
                case '~':
                //code to run the function whos name matches the input example
                //What follows is meta-code
                    functions = true;
                    break;
            }
        }
        private void handle_text()//this gets called by pressing enter
    // or pressing the enter button @ the textBox_input()
        {
            string inp = textBox_input.Text;
            if(!functions)
                input(inp); //this parses regular input
            else
            {
                rFunc(inp); //this should run functions
                functions = false; //reset it
             }
        }
    #region input
    //input function
    #endregion
        private void rFunc(string inp)
        {
        //What to put here?
        }
    };
    Right now I see no way of doing this, but there must be a way, considering debug consoles exist. Please help.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    You have to use reflection:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    
    namespace ConsoleApplication6
    {
        class Program
        {
            static void Main(string[] args)
            {
                string name;
    
                do
                {
                    Console.Write("Enter function name: ");
                    name = Console.ReadLine();
    
                    MethodInfo method = typeof(Program).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).FirstOrDefault(mi => mi.Name == name);
                    if (method == null)
                        Console.WriteLine("No method {0} exists.", name);
                    else
                        method.Invoke(null, null);
                } while (name != "");
            }
    
            private static void Method1()
            {
                Console.WriteLine("This is method 1");
            }
    
            private static void Method2()
            {
                Console.WriteLine("This is method 2");
            }
        }
    }
    Output...
    Code:
    Enter function name: foo
    No method foo exists.
    Enter function name: Method2
    This is method 2
    Enter function name: bar
    No method bar exists.
    Enter function name: Method1
    This is method 1
    Enter function name:
    No method  exists.
    Press any key to continue . . .
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    Ok. So I use this ->
    Code:
    MethodInfo method = typeof(Program).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).FirstOrDefault(mi => mi.Name == name);
    Then it will return null if such a method doesnt exist.
    To invoke the method I use
    Code:
    method.Invoke(null, null);
    It would be great if you could post a link about more info about this object type.
    (MethodInfo)

    I'll try to decode it myself here, and please correct me if I'm wrong.

    -typeof(Program)
    This probably means that I'm looking for a method inside my program?

    -GetMethods()
    This searches through the methods in the program according to the paramteres.

    -BindingFlags
    These specify for what types of methods I search for. Do they have to be static though?
    Code:
    FirstOrDefault(mi => mi.Name == name)
    this probably gets the first method in the list (or first overload) that maches the name the user put in...

    Now a few questions =D

    1. Can I use non-static and Public methods for this?
    2. How would I reference a method from another class?
    3. Is it possible to pass arguments? for example

    Code:
    Method2(string output)
    {
    Console.WriteLine(output);
    }
    and call it with
    Code:
    input: Method2("Read me!")
    ?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    All of these questions are readily answered with simple google searches. Let me google that for you

    EDIT: Alright, except the LINQ question.

    For information on "FirstOrDefault(mi => mi.Name == name)", check out http://msdn.microsoft.com/en-us/library/bb340482.aspx
    Last edited by itsme86; 08-23-2011 at 10:36 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    Code:
    public void function(string input)//DEBUG CONSOLE
            {
                string[] parts = input.Split('|');
                List<string> all = new List<string>();
                bool rest = false;
                foreach (string part in parts)
                {
                    if (rest)
                        all.Add(part);
                    if (!rest)
                        rest = true;
                }
                MethodInfo method = typeof(engine).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).FirstOrDefault(mi => mi.Name == parts[0]);
                if (method == null)
                    outform.output("No such method exists: " + input);
                else
                {
                    method.Invoke(null, all.ToArray());
    //update info
                }
                return;
            }
    this should be it for passing arguments =D
    Thanks for the help itsme86!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Creation of an OS
    By jrahhali in forum Tech Board
    Replies: 8
    Last Post: 09-18-2010, 07:04 AM
  2. Replies: 2
    Last Post: 12-16-2008, 01:43 PM
  3. Thread creation
    By telltoarun in forum C++ Programming
    Replies: 3
    Last Post: 01-06-2007, 07:00 AM
  4. Dev C++ DLL creation.
    By tol in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2005, 12:59 PM
  5. Win32 Console App... multiple console buffers
    By Trauts in forum Windows Programming
    Replies: 2
    Last Post: 04-28-2003, 11:26 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21