tags:

views:

328

answers:

6

i'm playing around with c# reflection api. i can easily load type info of classes, methods etc. in an assembly.

now i wonder how can i load and read the code inside a method?

+8  A: 

Basic Answer:

You can't with the reflection API (System.Reflection).

The reason is that the reflection api is designed to work on Metadata (Type of Classes, Name and Signature of Methods, ...) but not on the data level (which would be the IL-stream itself).

Extended Answer:

You can emit (but not read) IL with System.Reflection.Emit (e.g. ILGenerator Class).

Through MethodInfo.GetMethodBody() you can get the binary IL-stream for the implementation of a method. But thats usually completely useless by itself.

There are external libraries (like Cecil) that you can use to read/modify/add/delete code inside a method.

Foxfire
Can't you refer to the method via a delegate, then create an ExpressionTree from the delegate and inspect the tree?
Matt Greer
@Matt Greer: No. You cannot "reverse engineer" a delegate's method body into an expression tree.
LBushkin
@Matt: That's not going to give you the code *inside* the method, you'll just get a `MethodCallExpression` that gives you access to the same `MethodInfo` you could have gotten more directly with the reflection API.
Aaronaught
No unless it could parse a binary IL stream which it cannot.
Foxfire
@Foxfire: Hi. I don't know if you'll see this, but if you do... Could you quote your sources when you say that the reflection API is designed to work on Metadata, not on the data level? I believe you ;) but I'd like to include this in my master thesis and I need some kind of reference for it. Thanks so much :)
Alix
No, I don't have any scientific-citeable sources for this ready. But it is A) obvious B) you can easily find MS employees blog posts on this.
Foxfire
A: 

No
This is a feature slated for the next version of C#. You can use the CodeDom to get more info than reflection, but you cannot interrogate the parse tree yet.

Well there is always mono, in mono the compiler is a service, and you could get the parse trees at runtime.

The better question is why you want to?

DevelopingChris
A: 

Yes, there must be a way to achieve this: The .NET Reflector tool does this, too. Can't tell you how it's done there, though.

Sebastian P.R. Gingter
Why don't you reflect the reflector ;)
ntziolis
Reflector does not use reflection at least not in the .NET framework sense of reflection. It actually parses the PE file and dissembles the IL code from the .TEXT section. Then does some very smart and cool pattern matching to map/decompile the IL back to a target language.
Chris Taylor
A: 

If you don't need to do this real-time, have a look at Reflector. You can disassemble any .NET assembly (including the MS core DLLs) and see the code in your language of choice. This can be *very educational.

Update Has anyone tried using Reflector on Reflector to figure out how this is done?

David Lively
Note, this doesn't de-optimize the code. So if there was a compiler optimization done to the IL, it doesn't get returned to its original build source, but to a slightly less human written optimized version. Its a neat way to see what you could be doing most efficiently.
DevelopingChris
You can't use Reflector on Reflector. Try it and see, you'll get a nice surprise.
Aaronaught
Reflector is encrypted such that it can't reflect on itself, I think they used DotFuscator to do it.
DevelopingChris
+3  A: 

That depends on what you mean by read the code. There are 4 forms of the code.

1- The source code eg. the original C# or VB.NET - No you cannot get this with reflection
2- The symbolic IL code - No you cannot get this with reflection
3- The JITed assembly code - No you cannot get this with reflection

4- The IL bytes, the actual bytes that IL is compiled to, this you can get.

Take a look at MethodBase.GetMethodBody() for example, you can get the IL bytes, the local variables, exception frames etc. http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getmethodbody.aspx

Chris Taylor
`GetMethodBody()` is the closest you can get, I think.
LBushkin
A: 

You sort of can. The relevant function is MethodBase.GetMethodBody.

It's not exactly the most useful API. You can get some basic information about what's inside the method, and you can obtain the IL as a byte array. That's about it.

There's a slightly better API in the Mono.Cecil library, which exposes a MethodDefinition class with its own MethodBody implementation which contains actual Instructions, so you don't have to interpret the raw byte code. Still, if you're looking to get C# code out of it à la Reflector, you're going to be sorely disappointed. Also, Cecil isn't very well documented.

If you still want to try, then good luck.

Aaronaught