Medir la cantidad de métodos en una clase

I am supposed to count the number of methods in a class of any .NET program. For this I am making use of the il of that particular program. Initially I thought that I would count the number of "ret" in the il. But then I found out that even method declarations consists of the "ret" keyword. Then I tried to go with "callvirt". But this would not work in all the cases. Now I am thinking about something like this :-

Count the number of times the following appears in the il:- end of method class_name::method_name

but i don't know how to implement this (I am a complete newbie to .NET)

Can you kindly suggest an alternate way to identify a method in a class by making use of the il?

preguntado el 10 de marzo de 12 a las 11:03

3 Respuestas

My preferred methods:

Método 1:
Load the assemby into your AppDomain and use reflection. This executes code from the assembly(i.e. it should not be evil), and its dependencies must be resolvable too.

Método 2:
Load the assembly containing the class into Mono.Cecil, and then just enumerate the methods using its API.

Which of the two to choose depends on the context. If you have loaded/will load the assembly anyways, then use reflection. If you just want to analyze an assembly file, use Mono.Cecil.

Otros metodos:

Método 3:
Utilizan CCI, I have no experience with this one. Looks similar to Mono.Cecil.

Método 4:
Use reflection-only assembly loading with reflection. I found this one to be painful, and recommend not using it.

respondido 10 mar '12, 12:03

Dos formas de hacer esto:

//with an instance of the class
var instance = new YourClass();
var methods = instance.GetType().GetMethods();
var count = methods.Length;

//using the type of the class (no instance)
var classType = typeof(YourClass);
var methods = classType.GetMethods();
var count = methods.Length;

I've written this in several steps to make it understandable. Of course you can write it in one line. For example the second method looks like this then:

var count = typeof(YourClass).GetMethods().Length;

¡Espero que esto ayude! :)

respondido 10 mar '12, 11:03

I have tried using this but for some reason its giving the wrong count - Pavani gaddam

What are the number of actual methods and what is the result of the method I proposed? - Abbas

What are the methods you get back? Maybe it's also counting the methods from the class 'object' as all classes derive from this base-class.. - Abbas

its just giving the count and nothing else. So now instead of using the il i am using the program with a diff logic :) - Pavani gaddam

Off course it's returning the number, if you remove the 'Length' you'll get the methods themselves.. :) - Abbas

As you're talking about IL, I assume you want to count the number of methods in a compiled assembly.

You could load your .Net assembly then use reflection to retrieve all methods.

Vea Get class methods using reflection

contestado el 23 de mayo de 17 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.