-3

Is there a way for me to be returned a list of all methods of a target class in the form of an array of strings at runtime in C# other than parsing all the documentation for that class? I want to emulate what some IDE's do with suggesting methods as someone types a code segment.

So a method that would work (ideally) like so:

getMethods(foo.GetType())

And I would be returned a list of foo's methods.

  • Are you trying to implement auto complete feature? Use a library which does this already instead of reinventing the wheel. – Sriram Sakthivel Apr 01 '15 at 16:01
  • I can't implement an autocomplete feature unless I already know what methods to potentially autocomplete to. Unless you're talking about libraries that already have those built in. – Jackson Stone Apr 01 '15 at 16:07
  • you can use reflection to get the methods available on a type. – Levesque Apr 01 '15 at 16:13
  • See if [this helps you](https://github.com/lukebuehler/NRefactory-Completion-Sample). You don't have to manually write this. Your search keyword is "Code Completion C#" – Sriram Sakthivel Apr 02 '15 at 06:59

1 Answers1

0
using System;
using System.Linq;

namespace ConsoleApplication1
{
    class SomeClass
    {
        public void A() { }
        public void B() { }
        public void C() { }
        public void D() { }
        public void E() { }
        public void F() { }
    }

    class Program
    {
        static void Main()
        {
            var methods = GetMethods(typeof (SomeClass));

            foreach (var methodName in methods)
            {
                Console.WriteLine(methodName);
            }
        }

        private static string[] GetMethods(Type type)
        {
            var methods = type.GetMethods();
            return methods.Select(m => m.Name).ToArray();
        }
    }
}
James R.
  • 822
  • 8
  • 17