1. Delegation is the encapsulation of methods (static methods and instance methods). After we pass a method to the delegate
The delegate points to a specific method. Writing a parameter in the delegate is like writing a parameter in the method and pointing to
Same method.
2. declare that the delegate compiler will delegate this lineCodeGeneration, four methods: A constructor, invoke (and Source
The prototype specified in the Code is the same, with different names and virtual methods. parameters, return values, andSource codeConsistent definitions
), Begininvoke, and endinovke (begininvoke and endinovke are used for asynchronous callback ).
3. Therefore, each delegate has a constructor, And the constructor accepts two parameters: one object reference, one
The integer that points to the callback method.
4. Therefore, all delegates are inherited from multicastdelegate, while multicastdelegate inherits from
Delegate and delegate inherit from the object. It has three important fields: _ target (Object Class
Type), _ methodptr (int32), an internal number (to be precise, the field type is system.
Intptr, which is mainly used to indicate pointers or handles). CLR uses it to identify the callback method. , _ Prev
(Multicastdelegate) points to another delegate object. This field is usually null.
5. Compare whether the delegate uses equals, and check whether their _ traget, _ methodptr are equal and
_ Prev points to the linked list with the same length. If all matches, true is returned; even if it points to the same callback target/callback
Delegate objects of methods, even if they reference two different objects, but they all reference the same
So true is returned.
6. The delegate class defines three static methods to help us operate the delegate linked list and two combine
, A remove, all of which return value type is delegate static method.
7. + = call the delegate. Combine method, and use the-= delimiter method delegate. Remove.