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):
SomeDelegate
class is populated and the runtime stumbles upon theInvoke
method.PreStubWorker
(vm\prestub.cpp
) is called, which callsDoPrestub
, which callsMakeStubWorker
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 callsCOMDelegate::GetInvokeMethodStub
(vm\comdelegate.cp
p) to create the stub.COMDelegate::GetInvokeMethodStub
method (vm\comdelegate.cpp
) callsCOMDelegate::TheDelegateInvokeStub
which calls theEmitDelegateInvoke
andEmitMulticastInvoke
methods.StubLinkerCPU::EmitDelegateInvoke
andStubLinkerCPU::EmitMulticastInvoke
methods are implemented in thevm\i386\stublinkerx86.cpp
file (for x86) andvm\ppc\cgenppc.cpp
(for PowerPC). These methods are quite short and emit the concrete assembly/CPU-specific implementations of theInvoke
methods.SomeDelegate
's method table.