My upcoming Tech-Ed Talk in
For the past three weeks or so I’ve been totally immersed in Reflection-Land.
There are seriously cool things going on behind the scenes, and, frankly, I was surprised at the amount of new stuff that is now possible.
One of my favorite features is the new ability to reflection over a Method’s Body. That is, get the actually IL for the method in the form of a Byte Array. You can then parse the IL and do what ever you want with it (including building your own version of reflector, for example).
This is possible due to a new API added on the MethodBase class, called: “GetMethodBody()”. You can then use the resulting “MethodBody” object and invoke it’s “GetILAsByteArray()” method.
It’s really quite simple.
Parsing the Byte Array is a different matter, though. I’ll talk about that in a future post. For now, suffice to say that you can do wonderful things once you have the IL.
You can basically get the IL from any MethodInfo object at runtime and parse it (for security reasons, you might wanna make sure that there are no method calls to a specific method from any plugin you are about to load)
I was also able to create a MethodInfo Debugger Visualizer, based on the code released here by a guy from the Reflection team. His visualizer shows the IL for a DynamicMethod object at debug time (that’s also a new class I’ll talk about in a future post). It took me a day or so to figure out how to extend it to show the IL for any method info object during debugging, and it works quite well!
In fact, you can now write a method that writes its own code in IL J
All that and more, in the near future, I promise J
Meanwhile, here’s a nice post form Joel Pobar, highlighting some of the new features in Reflection 2.0. I’ll be elaborating on these more, but you can follow the links on his post as well to discover a whole new world of possibilities.
Cross Posted from Blogs.Microsoft.Co.Il/Blogs/Royo