Delegate class
It is delegated that the delegate is a data structure that references a static method or a reference class instance and an instance method of such a class.
System.Object System.delegate System.MulticastDelegate
The Delegate class is the base class of the delegate type. However, only the system and compiler can explicitly delete from the Delegate class or the MulticastDelegate class. In addition, new types are not allowed to derive new types from the principal type. The Delegate class is not a delegate type, which is used to derive the delegation type.
Most languages implements the delegate keyword, which is derived from the MulticastDelegate class. Therefore, the user should use the delegate keyword provided by the language.
The declaration of the delegate type established an agreement that specifies the signature of one or more methods. The delegate is an instance of the delegate type, which references one or more of the following: not a target object of the Visual Basic and the instance method of the target object. The static method is only a reference to the method only when the signature of the method is fully matched with the signature specified by the delegate type. When a delegate reference instance method, the delegate stores a reference to the entry point of the method and a reference to the object of the target, which is a class instance that calls the method. The goal of the instance method cannot be a nap reference. When the delegate references the static method, the delegate stores a reference to the entry point of the method. The target of the static method is a nap reference.
The delegated call list is the sorted commission set, where each element of the list happens to call a method of the delegate call. The call list can contain a repetition method. During the call, the entrustment is called in the order in which the method is in the call list. The entrustment attempts to call each method in the list, and the repetition method is called once in the call list. The entrustment is not changeable; once created, the delegated call list cannot be changed.
The commission can be multiple broadcast (composite) or single broadcast (uncommitted). Multi-Broadcasting (composite) delegate calls one or more methods and can be used to merge operations. Single Broadcast (incommptable) entrusting can only call a method and is not available for merge operations. The call list of the single broadcast commission A contains only one element, that is, reference to A.
Merging operations (such as Combine and Remove do not change the existing delegation. Instead, such an operation returns a new delegate, which contains the results of the operation, the unmodified delegate or empty reference (Nothing). When the result of the merge operation is not a commissioned commission, the operation returns a null reference. When the requested operation is invalid, the merge operation returns a delegate unnamled.
If the modified method triggers an exception, the method stops execution and passes the exception to the debused call, and no longer calls the rest of the rest in the call list. The exception of the capture call does not change this behavior.
When the signature of the method called the method is included in the return value, the delegate returns the return value of the last element in the call list. When the signature contains parameters passed by reference, the final value of the parameter is the result of each method in the call list, which sequentially performs and updates the value of the parameter.
The compiler provides two other ways to entrust: in C or C , the most similarity is the function pointer. However, the function pointer can only reference the static function, and the commission can reference the static method and instance method. When a delegate reference instance method, the delegate not only stores reference to the method entry point, but also stores a reference to the class instance of the method to call the method. Unlike functional pointers, the delegation is object-oriented.
[Visual Basic] Imports SystemPublic Class SamplesDelegate 'Declares a delegate for a method that takes in an int and returns a String. Delegate Function myMethodDelegate (myInt As Integer) As [String]' Defines some methods to which the delegate can point. Public Class mySampleClass 'Defines an instance method. Public Function myStringMethod (myInt As Integer) As [String] If myInt> 0 Then Return "positive" End If If myInt <0 Then Return "negative" End If Return "zero" End Function' myStringMethod
'. Defines a static method Public Shared Function mySignMethod (myInt As Integer) As [String] If myInt> 0 Then Return " " End If If myInt <0 Then Return "-" End If Return "" End Function' mySignMethod End Class 'MySampleClass
Public Shared Sub Main ()
'Creates one delegate for each method. Dim mySC As New mySampleClass () Dim myD1 As New myMethodDelegate (AddressOf mySC.myStringMethod) Dim myD2 As New myMethodDelegate (AddressOf mySampleClass.mySignMethod)
'Invokes the delegates. Console.writeline ("{0} is {1}; use the Sign" {2} "" "", 5, myd1 (5), myd2 (5)) console.writeline ("{0 } is {1}; use the Sign "{2}" "", - 3, myd1 (- 3), myd2 (- 3)) console.writeline ("{0} is {1}; use the Sign "{2}" "", 0, myd1 (0), myd2 (0)) End Sub 'main
End class' SamplesDelegate
'This code producer t stocking output:' '5 is posient; use the Sign " ".'- 3 is negative; use the sign "-".' 0 is zero; use the Sign "".