1

I have a list of "commands" for a console-line application that I'm developing. One of them is a method that prints all available commands for the player to use. I've tried calling Type.GetMethods on the CommandsList class in order to list them all, but returnString below is empty. After using a breakpoint, I found that commands is empty as well, even though public methods exist in CommandsList. This is the first time I've tried using Type.GetMethods, so I'm sure I'm doing something wrong. If someone can help me fix this, I would appreciate it. Below is my code:

using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;

public static class CommandsList {
    //(Other commands)
    public static void Commands() {
        MethodInfo[] commands = typeof(CommandsList).GetMethods(BindingFlags.Public);
        string returnString = "";
        foreach(MethodInfo info in commands) {
            returnString += info.Name + " ";
        }
        Console.WriteLine(returnString);
    }
}
Caleb Libby
  • 122
  • 10

1 Answers1

4

You need to tell the command you want static methods:

var commands = typeof(CommandsList).GetMethods(BindingFlags.Public | BindingFlags.Static);
stuartd
  • 70,509
  • 14
  • 132
  • 163