I have the following dump of delegate object:
Name: MyEventHandler
MethodTable: 132648fc
EEClass: 1319e2b4
Size: 32(0x20) bytes
Fields:
MT Field Offset Type VT Attr Value Name
790fd0f0 40000ff 4 System.Object 0 instance 014037a4 _target
7910ebc8 4000100 8 ...ection.MethodBase 0 instance 00000000 _methodBase
791016bc 4000101 c System.IntPtr 1 instance 2ef38748 _methodPtr
791016bc 4000102 10 System.IntPtr 1 instance 0 _methodPtrAux
790fd0f0 400010c 14 System.Object 0 instance 00000000 _invocationList
791016bc 400010d 18 System.IntPtr 1 instance 0 _invocationCount
How can I get the name of the method, pointed by the delegate?
In my experience the suggestion offered by hakan doesn’t work. Here’s what I do.
The output shows that the attached handler is a member of the object pointed to by
_target. By dumping that you’ll get it’s method table.I have constructed a similar example, to illustrate:
In this case, I’ll look at the object at
02842d20.So the target type is
app.Foo. Let’s dump the methods for this type.Compare the values of the
MethodDesctable with the original value of_methodPtr. No apparent match._methodPtr points to a piece of code which either does a
jmpto the address of the function in question or calls a fix-up routine, so the next step is to use the!ucommand on the value of_methodPtr. If we see ajmpinstruction, we have the address and by using!uon that, we get the method.If, on the other hand, we see a
calltoclr!PrecodeFixupThunkwe can get the MethodDesc by dumping the memory pointed to by_methodPtrlike thiswe see something that looks like a method table entry in as the third DWORD. By comparing the value
002c30a8with the method table above, we see that the name of the method isapp.Foo.Bar.Since this is a constructed example, I know that I have found the method, I was looking for in this case.
Actually it may be bit more complicated that the above example shows, as the fields are used differently depending on the actual usage of the event. However, in my experience the approach above will work in the general publisher/subscriber scenario.
For more details on the implementation details check out the file
comdelegate.cppof the shared source CLI.