How to parse an C# assembly and extract every method
Asked Answered
T

4

7

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)?

Tailrace answered 17/11, 2010 at 18:37 Comment(3)
What makes you think you can get source code out of an assembly?Hoarfrost
I think before you start to ask a question like this, you need to learn a little bit more about a pretty fundamental programming/cs topic called "compilation" :P. C# isn't interpreted.Rapport
Well, Reflector does a pretty good job of it.Conundrum
H
22

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();
Historicity answered 17/11, 2010 at 18:41 Comment(1)
Thanks for your answer. I edited the question since I dind't know that is not possible to extract source code from a assembly.Peregrinate
P
3

To retrieve this debugging information, use the Common Compiler Infrastructure: Metadata API project.

This allows you to read the .pdb files (the files containing the debugging information like source file and line number).

Polyphyletic answered 17/11, 2010 at 18:44 Comment(3)
Thanks for the hint, but where are these pdb files?Peregrinate
They are in the bin folder. These are the files where your filenames and lines are stored.Polyphyletic
@Roflcoptr - they are optional and make not exist, or may exist but only be available to the dev team (but not deployed)Historicity
L
2

First you need to find all the types.

See here to do that.

Then you can need to find all the methods for each types.

See here to do that.

If you need an example to bring it all together let me know.

Lackaday answered 17/11, 2010 at 18:41 Comment(1)
Thanks for your answer. I edited the question since I dind't know that is not possible to extract source code from a assembly.Peregrinate
C
1

I'm not sure how to get the source code, but if you're trying to get information about the methods, you should use reflection.

MethodInfo[] methodInfos = typeof(MyClass).GetMethods(BindingFlags.Public |
                                                  BindingFlags.Static);
Childbed answered 17/11, 2010 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.