delegate.Invoke 是如何工作的?

How does delegate.Invoke work?(delegate.Invoke 是如何工作的?)

本文介绍了delegate.Invoke 是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在我的代码中创建一个委托,例如:

If I create a delegate in my code like :

delegate void dostuff (string o);

这会生成一个派生自 System.MulticastDelegate 的类,该类实现了三个方法 - InvokeBeginInvokeEndInvoke.

This generates a class that derives from System.MulticastDelegate which implements three methods - Invoke, BeginInvoke and EndInvoke.

如果我查看为 Invoke 编译的 IL,我看到的是:

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.

使调用 Invoke 实际调用委托的巫毒从何而来?

Where does the voodoo that makes calling Invoke actually call the delegate come from?

推荐答案

voodoo 可以在签名的末尾找到:runtime managed.请注意,您定义的所有托管类和方法都将被修饰为 cli managed.

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 as cli managed.

runtime managed 表示运行时提供方法的预优化实现.

runtime managed means that the runtime provides pre-optimized implementations of the methods.

这篇关于delegate.Invoke 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:delegate.Invoke 是如何工作的?

基础教程推荐