How to parse an C# assembly and extract every method

anon picture anon · Nov 17, 2010 · Viewed 7.7k times · Source

For my application I'd like to parse through an assembly and extract every method and store the name of the method and the source code in one of my objects (I defined in my code).

where should I start to implement that?

EDIT: From the answers & comments i saw that it is not so easy to get source code from the assemblies. Then where should I start if I want to get source code from source code files that are not in my current solution (visual studio)?

Answer

Marc Gravell picture Marc Gravell · Nov 17, 2010

The assembly doesn't contain the source code. You might be able to extract the IL, but without tools like reflector or ildasm that isn't especially helpful.

To obtain the method names, just something like:

    var assembly = Assembly.GetExecutingAssembly();
    var names = (from type in assembly.GetTypes()
                 from method in type.GetMethods(
                   BindingFlags.Public | BindingFlags.NonPublic |
                   BindingFlags.Instance | BindingFlags.Static)
                 select type.FullName + ":" + method.Name).Distinct().ToList();