How to list methods of a class in C#?

Asked

Viewed 618 times

13

Again I have to say this: I come from PHP and am learning C# now.

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

$a = new ArrayObject();

get_class_methods($a);

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

2 answers

12

Dude, use the Type.GetMethods, I took the answer from Stackoverflow gringo himself 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);
        }
   }
}

This is the way out:

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)
  • 2

    I liked the answer and realized that not only are the methods listed, but also the arguments. But, if you do not mind my sincerity, the answer lacks more details, explaining the use, because in my case, I already have experience with programming, but a person who is starting now would not understand.

  • 2

    kkkkk No problem, I just took a look at the gringo overflow stack to answer you because I honestly never had the need to list the methods of a class. Usually if I want to debug or discover something about the methods Intelisense from Visual Studio helps me with this.

10


This is done with reflection. Specifically with the method GetMethods() class Type. It is possible to filter them as you wish, either through the method itself or with the array of the kind 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);
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

In this case I’m taking all the public instance methods and showing their simple name. Various method data can be taken, see documentation to see everything you can use.

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

Since all 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 archive Assembly to inform this. It is not something magical, it does not search in documentation, it is fact real of the type. You can pick up virtually any information you want about . NET codes, yours or third party codes.

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

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

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

  • I’m going to test using the interactive mode of C#, which I learned in the other answer. Thanks, bigown. It’s helping me evolve in C#

  • Great junction of things. If I am in patience I make a cool code to list the method in a well organized way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.