This project is read-only.

Getting ISourceMethodBody from MethodInfo

Mar 27, 2013 at 10:38 PM
I wanted to see if I could decompile a method without having to read the PE-file itself, but rather by getting it from reflection.
So I have a MethodInfo and I want to turn it into a ISourceMethodBody.
I've been poking around the CCI projects, trying to see what I would have to implement but I got a little lost in all the details. So I figured I would simply ask if this is going to be possible, or if this is something that would require me to re-implement large parts of the CCI Metadata/AST projects?

I was hoping there would be an ILReader that I could use with the results of MethodInfo.GetMethodBody() and then feed that into the decompiler, but it doesn't look like it will be quite that simple.

I started mocking out some code, and ended up with this, but the "IMetadataHost" and "IMethodBody" doesn't seem that easy to come by.
IMetadataHost host = ...;

IMethodBody methodBody = ...;

ISourceMethodBody sourceMethodBody = Decompiler.GetCodeModelFromMetadataModel(host,
                                                                              /* pdbreader */ null,
Any suggestions, or is this simply outside the bounds of the CCI projects?

Apr 20, 2013 at 8:16 PM
This is not out of the bounds of the project, but the crucial missing piece is a Reflection to CCI mapper. I.e. an inverse for the ReflectionEmitter project. Unfortunately this is not a small undertaking. I started doing that a while back while I was still at Microsoft, but never got the time to complete it.

Since then, the time I can spend on CCI has dramatically decreased, so I don't have much hope that I can provide you with this missing puzzle piece.
Apr 26, 2013 at 11:48 AM
That's good to hear, I don't suppose there is a nice starting point for this, as in "everything begins with an 'IXxxProvider'", that way I could start slowly building it out from that point. Or is this a completely independent piece of functionality that has to live on it's own? If you already started on this, would it be possible for me to pick
that up? Or does it requires extensive understanding of how CCI AST works? (I'm fairly familiar with reflection, so that shouldn't be an issue).
Apr 27, 2013 at 4:48 PM
I'm afraid that I no longer have access to the source code for that project. Perhaps my former colleagues will be kind enough to post it. You only need to understand Reflection and the CCI metadata model to provide this puzzle piece.
May 6, 2013 at 6:01 AM
Mike has kindly posted the code. Look for the new ReflectionImporter project.