-1

I'm in a situation where I only have a reference to the managed Assembly Reference object in-memory, i.e. I don't have the dll or actual code. This whole question is about the fact that I have the in-memory representation of System.Reflection.Assembly.

I want to be able to disassemble it to text, or at least get the metadata also for let's say a function body.

Is this possible?

I've tried all the public surface area of the object and nothing gives.

svick
  • 236,525
  • 50
  • 385
  • 514
halivingston
  • 3,727
  • 4
  • 29
  • 42
  • So is what you are asking is that you have a reference to an assembly *in code* and you want to be able to disassemble that *in code*? Or do you have an assembly that you need disassembled and don't have to do it in code? – Ron Beyer Nov 15 '15 at 03:14
  • I have in memory. Sorry, I should edit. I don't have it at compile time, or any other time other than when running inside a process. – halivingston Nov 15 '15 at 03:16
  • 1
    What specific metadata are you after? – Ron Beyer Nov 15 '15 at 03:19
  • strings. I want to find out all constant strings that are used in methods – halivingston Nov 15 '15 at 03:20
  • Maybe this may help: [Anatomy of a .NET Assembly](https://www.simple-talk.com/blogs/2011/03/16/anatomy-of-a-net-assembly-clr-metadata-1/). If you have a reference to the assembly, you can get its file location on disk and open it directly. – Ron Beyer Nov 15 '15 at 03:25
  • Unfortunately, the assembly is not actually located on disk anywhere. – halivingston Nov 15 '15 at 03:39
  • It is obviously possible to "decompile" low-level code, whether it's JITted native code or IL from the managed assembly. Thus tools like dotPeek, Reflector, etc. (for managed code) and similar ones for native code. If you want someone to help with a _specific_ question related to that sort of thing, you need to provide [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that shows clearly what you've tried, with a precise explanation of what that code does and how that's different from what you want. – Peter Duniho Nov 15 '15 at 04:38

1 Answers1

1

I don't know about anything that would disassemble a whole in-memory assembly into text. And I don't know how could you save an in-memory assembly to disk to use a file-based disassembler (like ildasm).

But if all you want is to find out what constant strings are used by methods, you don't need all that.

To access the IL of a method (that you have as MethodInfo), you can use method.GetMethodBody().GetILAsByteArray(). As the method name implies, this gives you a byte[], which is not easy to work with. But you can use Jb Evain's Mono.Reflection to do the hard work for you (despite the name, it's not really related to Mono and can be used from normal .Net too).

The only step remaining is to realize that constant strings are accessed using the ldstr instruction, so that's what we need to look for.

Knowing all this, finding the constant strings used by a method is as simple as:

method.GetInstructions().Where(i => i.OpCode == OpCodes.Ldstr).Select(i => (string)i.Operand)
svick
  • 236,525
  • 50
  • 385
  • 514