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 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.
– Wallace Maxters
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.
– JonesVG