My friend Jeffrey Richter sent me a really cryptic email several weeks ago. I had MSN messenger’d him a question about how one converts a MethodDef token into a MethodBase object. However, MSN Messenger does not really show you someone else’s status in real time, so he didn’t see my message until later. He replied using Hotmail since I had since logged off for the evening. His message contained a subject of “Does ldtoken do this?”
Now, when I finally got around to checking my Hotmail account, I didn’t really understand what he meant by this. When I ran into Jeffrey at VS Live a few weeks ago, he remembered that message and asked me if it had helped me out. At that time I had no idea what he was referring to. He reminded me about what I had asked him and a light bulb went off in my head.
When I got home, I took a closer look at the CIL documentation in the Partition III ECMA spec. Wow. That’s all I can say. Wow. The ldtoken instruction does exactly what I needed. It is a really unusual instruction since what it does is convert a MethodDef, TypeDef, and FieldDef token into a RuntimeMethodHandle, a RuntimeTypeHandle or a RuntimeFieldHandle object, respectively. You can use these handle objects to construct the appropriate System.Reflection objects at runtime.
Before we go any further, I need to explain why I even care about this instruction. Most of my CLR spelunking relates to solving real problems in my runtime aspect weaver. The problem that was sitting on the back-burner for a while was how to integrate my runtime aspect weaver with the System.Reflection API’s.
When I weave new code at some join-point, one of the pieces of information that I provide the advice method with is the name of the method that was weaved. In the M3 build of my runtime aspect weaver that I showed at VS Live!, I simply passed the name of the method as part of the CallContext object that I create in my dynamic proxy.
However, I wanted more power than that. What if the advice method wanted to reflect on that method to get more information about the context of the call? In order to enable this, I needed to provide the advice method with a System.Reflection.MethodBase object that represents the original method that was weaved. In my previous implementation of my CallContext object, I actually walked the stack to find the MethodBase object:
// Caller is always one past the dynamic interceptor on the call stack
protected MethodBase TargetMethod
{
get
{
StackTrace trace = new StackTrace();
if( trace.FrameCount < 2 )
throw new Exception( "Not enough stack frames in StackTrace!" );
StackFrame frame = trace.GetFrame( 2 );
return frame.GetMethod();
} }
Now this is a really crufty hack since I shouldn’t need to walk the call stack to get the MethodBase object. After all, I already know the MethodDef token of the method being weaved! So … what I needed to be able to do was to convert a MethodDef token into a MethodBase object. The key, as it turns out, is the RuntimeMethodHandle object.
To create a RuntimeMethodHandle object, you need to generate a ldtoken instruction. The ldtoken instruction also contains the token that needs to be encoded, so it is 5 bytes in length. Here’s an example from some experiments that I did today:
IL_0000: /* D0 | (06)000007 */ ldtoken method instance void Library.AnotherType/* 02000003 */::DoNothing() /* 06000007 */
As you can see, the MethodDef token (0x06000007) is part of the ldtoken instruction. This is not a problem for my runtime aspect weaver, since I need to generate the dynamic proxies on the fly, so generating custom ldtoken instructions is very straightforward.
To obtain a MethodBase object from a RuntimeMethodHandle object, all you need to do is call the static GetMethodFromHandle() method:
MethodBase method = MethodBase.GetMethodFromHandle( handle );
As you can see, ldtoken is a very powerful instruction. Unfortunately, it isn’t really useful for folks other than compiler writers since you need to dynamically emit this instruction at runtime. It would be nice, however, if there were a new method on System.Reflection.Module that would let folks look up MethodBase objects based on a MethodDef token; this would provide valuable integration with the unmanaged metadata API’s.