How to list methods of a class in C #?

13

Once again I have to say this: I come from PHP and I'm learning C # now.

I usually like to list the methods the class has, since I always did this in PHP, to test or debug.

$a = new ArrayObject();

get_class_methods($a);

What about C #? How can I do to list the methods of an object?

    
asked by anonymous 17.05.2016 / 22:21

2 answers

10

This is done with reflection. Specifically with the GetMethods() method of the Type . You can filter them as you want, either through the method itself or later with the array of type MethodInfo generated by it.

Examples:

objeto.GetType().GetMethods() //resolve o tipo em tempo de execução
typeof(TipoAqui).GetMethods() //resolve o tipo em tempo de compilação

Real example:

foreach (var method in typeof(String).GetMethods(BindingFlags.Public | BindingFlags.Instance)) {
    WriteLine(method.Name);
}

See running on dotNetFiddle and on CodingGround .

In this case I'm picking up all public instance methods and showing their simple names. Various method data can be taken, see documentation for everything you can use.

Obviously some of the members of the MethoInfo class have more complex information than a simple string , or a Boolean (several properties are thus to indicate a method quality - how the method was declared), it can have another array with information about other method members, for example the parameters in it, the attributes of it, as can be seen in another question .

Since all of this information is in collections, it is very common to use LINQ to filter the way you want.

It uses the metadata of the existing types in the assembly file to report this. It's not magic, it's not documentation, it's a real fact of the sort. You can get virtually any information you want about your .Net or third party codes.

Note that you only get the implemented members in the type. If you want the methods that the type has access because it inherited from the others and they were not overwritten, it has to get the base type (there is a method that helps to do this).

It is possible to get all members of the type, not just the methods. See the documentation listed above.

I made a sample that takes some data from the method . It is very simple and does not handle errors, so type the full name of the type (including the namespace ). is a basis of what would be a part of a decompiler.

    
17.05.2016 / 22:28
12

Dude, use Type.GetMethods , I removed the answer from StackOverflow itself and it works, at least here.

StackOverflow Gringo

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);
        }
   }
}

The output will be this here:

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)
    
17.05.2016 / 22:28