0

I have a requirement to list out the methods in the cs files and list the each method call inside a method . For example : i have a class

public void method1 ()
{
    c2.M2();
}

public void method2 ()
{
    c2.m3();
}

Here c2 is another class and m2 m3 are the methods of that class. Here i need to prepare a list c2.m2(), and c2.m3() , i have one hundred or more cs files to list out these details.

Is there any way i can effectively navigate through cs files and search instead of loading them and dealing with file stream readers?

ansar
  • 128
  • 13

1 Answers1

1

Two different ways of getting what you're looking for:

  • Mono.Cecil looks at compiled assemblies (like Reflection and/or ILDasm) and lets you inspect the IL instructions inside method bodies.
  • Roslyn looks at .cs source files and gives you a parse tree.

Either one should let you list the calls made inside each method.

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • Hi , Using Mono.Cecil it is of licensed version and has to be purchased , even i have tried with the sample code i was able to extract the c# format of a method but not the list of method calls inside a method, if i had to extract a method code i can use this one but unfortunately i have cs files need to list out the method calls. If you have a sample code of listing a methods inside a method using Cecil will be very helpful – ansar Apr 03 '13 at 12:15
  • I don't know where you got the idea that Mono.Cecil needs to be purchased -- it's open-source (their Web site doesn't seem to mention the license, but you can see it in the [license header](https://github.com/jbevain/cecil/blob/master/Mono.Cecil/ArrayType.cs) in the source files; it's the [MIT license](http://opensource.org/licenses/MIT)). As for how to use Cecil, try starting with the [Cecil FAQ](http://www.mono-project.com/Cecil:FAQ) and the [`mono.cecil` tag on StackOverflow](http://stackoverflow.com/questions/tagged/mono.cecil). – Joe White Apr 07 '13 at 02:37
  • Sorry i was misunderstood the tools between Reflector and Mono.cecil,thanks for your reply and i have seen the code but not found any useful for my requirement i needed to list out the methods called inside the methods in the classes but by this Mono.cecil i could able to extract the instructions. please provide some help. Can i get any help with the Nunit framework ?.. – ansar Apr 08 '13 at 10:00
  • The Cecil FAQ tells you two different ways to get the instructions inside a method body. From there it should be a matter of looking for `call` and `callvirt` instructions (and possibly `newobj` or a few others, depending on your requirements). What have you tried, and what are the specific problems you're running into? – Joe White Apr 08 '13 at 12:06
  • I have tried this Cecil faq only but instructions are in the format of Opcodes and values are in integers it is quite confusing. how to get the c# format of instructions oras you have mentioned call and callvirt instructions has to be separated how to do that . a simple example can help me . – ansar Apr 09 '13 at 11:36
  • Roslyn is not avaialable for VS 2010 . – ansar Aug 16 '13 at 12:48
  • and cecil will have OpCodes and instructions as a format. both are not helpful in my case Can anyone help me on this .? – ansar Aug 16 '13 at 12:53