如何delegate.Invoke工作?工作、delegate、Invoke

2023-09-03 01:57:59 作者:南樱

如果我在code创建一个委托,如:

If I create a delegate in my code like :

delegate void dostuff (string o);

这会生成一个派生自 System.MulticastDelegate 它实现了三个方法的类 - 调用的BeginInvoke EndInvoke会

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

如果我把编译后的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

该方法不包含code。调用它的工作 - 委托被调用,但我看不出它是怎么做的。

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?

推荐答案

巫毒教,可以在签字的最后发现:运行管理。请注意,您的所有管理类和方法的定义将被装饰成 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 as cli managed.

运行管理表示运行时提供的方法pre-优化的实现。

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