Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate List of methods of a class with method types

Tags:

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

like image 205
cedric Avatar asked Jul 29 '09 06:07

cedric


People also ask

How many methods are there in a class?

b) A class should contain an average of less than 30 methods, resulting in up to 900 lines of code.

How do you get all the methods of a class in Python?

To list the methods for this class, one approach is to use the dir() function in Python. The dir() function will return all functions and properties of the class.

How do I get all the methods of a class in Visual Studio?

In Visual Studio 2015, View > Other Windows > Resource View. The keyboard shortcut is Ctrl + Shift + E .

How do you print a class method in Python?

In Python, this can be achieved by using __repr__ or __str__ methods. __repr__ is used if we need a detailed information for debugging while __str__ is used to print a string version for the users. Important Points about Printing: Python uses __repr__ method if there is no __str__ method.


2 Answers

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)

like image 52
Jon Skeet Avatar answered Sep 23 '22 03:09

Jon Skeet


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

like image 30
David Schmitt Avatar answered Sep 24 '22 03:09

David Schmitt