Where exactly is SomeDelegate.Invoke implemented and how is it “wired” to the delegate classes

柔情痞子 提交于 2019-12-11 03:57:45

问题


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.


回答1:


So, here is how the voodoo magic works (from what I found by glancing over the sources for an hour):

  1. At some time, the method table for the SomeDelegate class is populated and the runtime stumbles upon the Invoke method.
  2. The PreStubWorker (vm\prestub.cpp) is called, which calls DoPrestub, which calls MakeStubWorker
  3. 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.cpp) to create the stub.
  4. The COMDelegate::GetInvokeMethodStub method (vm\comdelegate.cpp) calls COMDelegate::TheDelegateInvokeStub which calls the EmitDelegateInvoke and EmitMulticastInvoke methods.
  5. 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.
  6. The reference to the method implementation is put to the SomeDelegate's method table.


来源:https://stackoverflow.com/questions/25095709/where-exactly-is-somedelegate-invoke-implemented-and-how-is-it-wired-to-the-de

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!