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
2009-07-29 06:51:24
+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
2009-07-29 06:51:24
thanks..this worked
cedric
2009-07-30 09:00:39
A:
This should do the trick.
In short - you can get information about class and it's methods through process called reflection.
Arnis L.
2009-07-29 06:52:25
A:
using (StreamWriter sw = new StreamWriter("C:/methods.txt"))
{
foreach (MethodInfo item in typeof(MyType).GetMethods())
{
sw.WriteLine(item.Name);
}
}
ArsenMkrt
2009-07-29 06:56:29