One. The use of the callback mechanism in delphi is implemented by a function pointer, that is, the number of information implemented by the address of the function, the type, the return value type and other information, in all, is non-type security. The callback mechanism in .NET is achieved by delegate. Entrusted is a type of safe callback mechanism and supports multiple transmission. Entrusted use (unicast commission): 1. Declaring the Public Delegate Void Callback (int myarg); we can see that there is an INT type parameter represented by the delegate, there is no reference to the value of the value, which ensures that the signature of the called method is correct, so it is type safe. 2. Instantiates Callback MyCallback = New Callback (MyClass.Feedback); 3. Call entrusting mycallback (3); multicast commission: The above commission contains only one method, if you want to call multiple methods, you can only explicitly call the delegate multiple times. Entrusted can also call multiple methods, which is called multicast commission. Delegate provides a static method combine method to add delegate to the commissioned chain, and the static method Remove removes the delegate. In order to mitigate the programming of the developer, the C # compiler automatically overloads = and - = operators for the delegate type instance. The C # compiler translates the two operators to the COMBINE and REMOVE methods (this can be verified with ILDASM.EXE tool). When using multicast commission, if the delegate object has a return value, only the return value of the last callback method is returned, and the other return values will be abandoned. Moreover, if the delegate is called for a long time or throws an abnormality? Since the object on the commission chain is called in sequence, a delegate object on the commission chain will hinder the implementation of other delegated objects on the commission chain. In order to solve this algorithm is not suitable, the MulticastDelegate class provides a GetInvocationList () method, which we can explicitly call the various delegate objects on the commission chain, which can be designed to call the entrust chain on the commission chain as needed. two. The use of the decryption of the commission is very simple, but things are not as simple as the above example. The compiler and CLR have made a lot of work in the background to hide the complexity of the problem itself. The following describes how the compiler and the CLR implement delegate. Take the public delegate void callback (int myarg) as an example: the compiler automatically generates a complete class definition: public Sealed class callback: system.multicastdelegate {public callback (Object: Object: 5> @Object, intptr
Several important fields are listed below: 1. Private Object