delegate.Invoke 如何工作?
如果我在代码中创建一个委托,如下所示:
delegate void dostuff (string o);
这会生成一个派生自 System.MulticastDelegate 的类,该类实现三个方法 - Invoke
、BeginInvoke
和EndInvoke
。
如果我查看 Invoke
的已编译 IL,我看到的只是:
.method public hidebysig newslot virtual
instance void Invoke(string o) runtime managed
{
} // end of method dostuff::Invoke
该方法不包含任何代码。调用它确实有效 - 委托被调用,但我看不到它是如何做到的。
使得调用 Invoke 实际上调用 delegate 的巫毒从何而来?
If I create a delegate in my code like :
delegate void dostuff (string o);
This generates a class that derives from System.MulticastDelegate
which implements three methods - Invoke
, BeginInvoke
and EndInvoke
.
If I look at the compiled IL for Invoke
all I see is :
.method public hidebysig newslot virtual
instance void Invoke(string o) runtime managed
{
} // end of method dostuff::Invoke
The method contains no code. Calling it does work - the delegate gets invoked, but I can't see how it does it.
Where does the voodoo that makes calling Invoke actually call the delegate come from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
伏都教可以在签名的末尾找到:
运行时管理
。请注意,您定义的所有托管类和方法都将被修饰为cli 托管
。运行时管理意味着运行时提供方法的预优化实现。
The voodoo can be found at the end of the signature:
runtime managed
. Notice that all of your managed classes and methods that you define will be decorated ascli managed
.runtime managed
means that the runtime provides pre-optimized implementations of the methods.