I have carefully studied the C # delegation and events over the past few days. I would like to summarize the research knowledge. It would be nice to find out after N years.
Historically, Windows APIs often use C-language function pointers to create entities called callback functions or called callbacks for short. With callback, codenon enables a function to return to another function in the (callback) program.
In. NET Framework, callback is still possible. Their functions are more secure and object-oriented.Delegate). Essentially, a delegate is a type-safe object that points to another method (or multiple methods) that will be called later in the program ). The delegate type contains three required information:
- The name of the method it calls;
- Parameters of this method (optional );
- Return Value of this method (optional );
After a delegate is created and the preceding information is provided, it can dynamically call the method it points to at runtime. As you can see ,. in NET Framwwork, each delegate (including custom delegate) is automatically granted the synchronous or asynchronous access method capability, instead of manually creating and managing a Thread object, you can directly call the method on another auxiliary execution Thread, which greatly simplifies programming.
Use C # To define the delegate: if we want to create a delegate named BinaryOp, it can point to any two integers to return an integer:
Public delegate int BinaryOp (int x, int y );
When the C # compiler processes the delegate type, it automatically generates a seal class derived from System. MulticastDelegaet. Together with its base class System. Degelate, this class provides the necessary infrastructure for the delegate to maintain the list of methods to be called in the future.
The words are suddenly no electricity, no storage, more painful than the need to change, no way, can only come back.
If we use ildasm.exe to view the BinaryOp delegate, we can see that the generated BinaryOp class defines three public methods. Invoke () may be the core method because it is used to call every method of delegate type maintenance in synchronous mode. The BeginInvoke () and EndInvoke () methods can call the current method asynchronously in the second execution thread.
How does the compiler know exactly how to define the Invoke (), BeginInvoke (), and EndInvoke () methods? For more information, see the following code:
BinaryOp( target, Invoke( x, IAsyncResult BeginInvoke( x, y,AsyncCallback cb,
It may be difficult to contact the delegate for the first time. Next, let's take a look at a very Simple example of using BinaryOp delegation. We have seen before that this is a console application project named Simple Delegate.
BinaryOp( x, Add( x, x + Substract( x, x - Main( BinaryOp d = ,
. Net delegation is type-safe. Therefore, if the reader attempts to pass a method of mismatched pattern into the delegate, the compilation error will be received. For example, assume that the SimpleMath class defines a method named SquareNumber:
SquareNumber( a*
Because the BinaryOp delegate public can point to a method with two Integer Parameters and return an integer, the following code is invalid and cannot be compiled:
BinaryOp b2= BinaryOp(SimpleMath.SquareNumber);
Delegate object
We create a static method named DisplayDegelateInfo () in the program type to enrich the current example. This method outputs The Name Of The method maintained by the input delegate type and the name of the class that defines the method. Iterate the System. Delegate array returned by GetInvocationList () and call the Target and method attributes of each object:
(Delegate d