tags:

views:

112

answers:

5

I want to generate a list of all methods in a class or in a directory of classes. I also need their return types. Outputting it to a textfile will do...Does anyone know of a tool, lug-in for VS or something which will do the task? Im using C# codes by the way and Visual Studio 2008 as IDE

+1  A: 

You can get at these lists very easily with reflection. e.g. with Type.GetMethods()

David Schmitt
+4  A: 

Sure - use Type.GetMethods(). You'll want to specify different binding flags to get non-public methods etc. This is a pretty crude but workable starting point:

using System;
using System.Linq;

class Test
{
    static void Main()
    {
        ShowMethods(typeof(DateTime));
    }

    static void ShowMethods(Type type)
    {
        foreach (var method in type.GetMethods())
        {
            var parameters = method.GetParameters();
            var parameterDescriptions = string.Join
                (", ", method.GetParameters()
                             .Select(x => x.ParameterType + " " + x.Name)
                             .ToArray());

            Console.WriteLine("{0} {1} ({2})",
                              method.ReturnType,
                              method.Name,
                              parameterDescriptions);
        }
    }
}

Output:

System.DateTime Add (System.TimeSpan value)
System.DateTime AddDays (System.Double value)
System.DateTime AddHours (System.Double value)
System.DateTime AddMilliseconds (System.Double value)
System.DateTime AddMinutes (System.Double value)
System.DateTime AddMonths (System.Int32 months)
System.DateTime AddSeconds (System.Double value)
System.DateTime AddTicks (System.Int64 value)
System.DateTime AddYears (System.Int32 value)
System.Int32 Compare (System.DateTime t1, System.DateTime t2)
System.Int32 CompareTo (System.Object value)
System.Int32 CompareTo (System.DateTime value)
System.Int32 DaysInMonth (System.Int32 year, System.Int32 month)

(etc)

Jon Skeet
thanks..this worked
cedric
A: 

This should do the trick.

In short - you can get information about class and it's methods through process called reflection.

Arnis L.
A: 
using (StreamWriter sw = new StreamWriter("C:/methods.txt"))
{
      foreach (MethodInfo item in typeof(MyType).GetMethods())
      {
            sw.WriteLine(item.Name);
      } 
}
ArsenMkrt