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?
Invoke(Delegate): Executes the specified delegate on the thread that owns the control's underlying window handle.
If you think of delegates as being similar to interface definitions for a specific type of method, you can start to see why delegates exist. They allow clients of our delegates to ignore all the details of their implementations - even their names!
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
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
means that the runtime provides pre-optimized implementations of the methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With