Entrusted in C #

xiaoxiao2021-03-06  15

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 methodPtr); public hidebysig virtual instance void Invoke (int32 'value'); public virtual IasyncResult BeginInvoke (int32 value, CallBack callback, Object object); public virtual void EndInvoke (IAsyncResult < URN: Object: 6> Result;} Because all delegates are inherited from system.multicastdelegate, nature also contains the fields in System.MulticastDelegate.

Several important fields are listed below: 1. Private Object _target; pointing to the callback function to be called the object that should be called. 2. Private INTPTR _methodptr; an internal integer value is used to identify the callback function. 3. Private MulticastDelegate _prev; pointing to another delegation. (By this field can form a delegate chain) all delegates have a constructor, and the constructor receives two parameters: an object reference, a internal int value of the callback method. Look at the parameters in our example are myclass.feedback, and there is no incoming object reference and the internal int value of the callback method. The reason is not wrong because the compiler returns to determine the source code to determine that object and method. The object reference passes to the parameter target, pointing to the internal INT value of the callback method to the parameter methodptr. For static methods, NULL will be passed to the Target. Inside the constructor, the two parameters are passed to _target and _methodptr, respectively. Each delegate object is actually a package of an object that is operated while the method and its calling method. MulticastDelegate defines two properties: Target and Method. Given a reference to a delegate object, we can view these properties. Let's take a look at how the callback method is called. Looking at the above example, we seem to call a MyCallback function and pass a parameter. But there is no MyCallback function. Because the compiler knows MyCallback is a variable to the delegate object, so it automatically adds code to call the INVOKE method of the entrusted object. At this point, we are not difficult to understand the use of the entrusted.

转载请注明原文地址:https://www.9cbs.com/read-47466.html

New Post(0)