Where exactly (CLR source file) can in find the actual implementation of the SomeDelegate.Invoke
method?
How does the .Net runtime knows that calling SomeDelegate.Invoke
should result in calling that implementation? Keep in mind that the SomeDelegate.Invoke
method can have any number of arguments.
So, here is how the voodoo magic works (from what I found by glancing over the sources for an hour):
- At some time, the method table for the
SomeDelegate
class is populated and the runtime stumbles upon the Invoke
method.
- The
PreStubWorker
(vm\prestub.cpp
) is called, which calls DoPrestub
, which calls MakeStubWorker
- The
MakeStubWorker
sees that the method is runtime-implemented (pMD->IsEEImpl
), asserts that the method table (why ask the method table?) looks like a delegate and calls COMDelegate::GetInvokeMethodStub
(vm\comdelegate.cp
p) to create the stub.
- The
COMDelegate::GetInvokeMethodStub
method (vm\comdelegate.cpp
) calls COMDelegate::TheDelegateInvokeStub
which calls the EmitDelegateInvoke
and EmitMulticastInvoke
methods.
- The
StubLinkerCPU::EmitDelegateInvoke
and StubLinkerCPU::EmitMulticastInvoke
methods are implemented in the vm\i386\stublinkerx86.cpp
file (for x86) and vm\ppc\cgenppc.cpp
(for PowerPC). These methods are quite short and emit the concrete assembly/CPU-specific implementations of the Invoke
methods.
- The reference to the method implementation is put to the
SomeDelegate
's method table.