Traverse a c# method and anazlye the method body
Asked Answered
T

1

8

Whats the easiest way to traverse a methodinfo in c#?

I want to traverse the method body and find field-references and such and retrieves the types.

In System.Reflection there is:

mi.GetMethodBody().GetILAsByteArray()

which is kinda low-level and would require "some" work before I would be able to traverse the body.

I know Cecil exists, but there's a problem in loading an in memory assembly with cecil. The assembly i'm working with is't always "on disk" it can be an in memory assembly compiled from eg. Boo, and I wan't a clean solution without writing the assembly temporary to disk.

What other alternatives is there out there for this?

Trigraph answered 5/8, 2010 at 10:22 Comment(2)
What's the problem with Cecil and in-memory assemblies?Consensual
@Tim: I found this markmail.org/message/yssmfgrsqnnnzrvt , it's a bit old. There is no method in the AssemblyFactory to load an existing assembly only from file, byte[] or Stream. I tried to serialize a Boo-generated assembly to byte[] but got "Type System.Reflection.Emit.AssemblyBuilder is not marked as Serializable." Maybe im doing something wrong with retrieveing a byte[] from an assembly.Trigraph
V
8

This thing should help: Reflection based CIL reader. It operates on byte array obtained by calling GetILAsByteArray()

Vaudois answered 5/8, 2010 at 10:47 Comment(3)
Thanks, that should do it. Must have missed that one when googled for a solution.Trigraph
At least you googled before asking. Not like so many here on the site ;-)Archaism
If anyone from the mono-team is reading this, there is a bug or something in the runtime: bugzilla.novell.com/show_bug.cgi?id=628660Trigraph

© 2022 - 2024 — McMap. All rights reserved.