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.