(Delegate's constructor, Multicastdelegate,begininvoke,endinvoke,invoke4 method of discussion)
First define a delegate as follows:
public delegate void MyEventHandler (int i,out string o);
Then use the MSIL Disassembler (Ildasm.exe) to view the disassembly code
Actually, the CLR did 4 things for us.
1. Define a builder
2. Define a virtual method BeginInvoke
3. Define a virtual method EndInvoke
4. Define a virtual method invoke
(1) In the disassembly code I see the following fragment:
. class Auto ANSI sealed nested public MyEventHandler
extends [mscorlib]system.multicastdelegate
{
}/End of class MyEventHandler
From this we can know that in fact we declare the commission MyEventHandler is a sealed class
Its parent class is system.multicastdelegate.
See here, one of my doubts was relieved
I used to define delegates myself, and I always felt like I could define delegates everywhere in the program, inside and outside of the class, and never know why.
Now I basically understand that a delegate is a class, where a class can be defined, where the delegate can be defined.
(2) Let's look at the constructor again.
Before that, it is necessary to understand the relationship between the entrusted succession
System.Delegate
---system.multicastdelegate
------Consoleapplication1.myeventhandler
Now we instantiate a delegate:
MyEventHandler my=new MyEventHandler (Staticcall);
It is actually calling the constructor of this delegate
The following is the code for this constructor:
Public MyEventHandler (Object @object, IntPtr method);
The first argument should be an instance of the class instance
The second argument should be the method information for the delegate
This constructor will call the constructor of its parent class again, and the parent constructor code is as follows:
Protected MulticastDelegate (object target, String method): base (target, method)
{
}