MCAD The delegation of learning notes

Source: Internet
Author: User
Tags constructor

(Delegate's constructor, Multicastdelegate,begininvoke,endinvoke,invoke4 method of discussion)

First define a delegate as follows:

public delegate void MyEventHandler (int i,out string o);

Then use the MSIL Disassembler (Ildasm.exe) to view the disassembly code

Actually, the CLR did 4 things for us.

1. Define a builder

2. Define a virtual method BeginInvoke

3. Define a virtual method EndInvoke

4. Define a virtual method invoke

(1) In the disassembly code I see the following fragment:

. class Auto ANSI sealed nested public MyEventHandler
extends [mscorlib]system.multicastdelegate
{
}/End of class MyEventHandler

From this we can know that in fact we declare the commission MyEventHandler is a sealed class

Its parent class is system.multicastdelegate.

See here, one of my doubts was relieved

I used to define delegates myself, and I always felt like I could define delegates everywhere in the program, inside and outside of the class, and never know why.

Now I basically understand that a delegate is a class, where a class can be defined, where the delegate can be defined.

(2) Let's look at the constructor again.

Before that, it is necessary to understand the relationship between the entrusted succession

System.Delegate

---system.multicastdelegate

------Consoleapplication1.myeventhandler

Now we instantiate a delegate:

MyEventHandler my=new MyEventHandler (Staticcall);

It is actually calling the constructor of this delegate

The following is the code for this constructor:

Public MyEventHandler (Object @object, IntPtr method);

The first argument should be an instance of the class instance

The second argument should be the method information for the delegate

This constructor will call the constructor of its parent class again, and the parent constructor code is as follows:

Protected MulticastDelegate (object target, String method): base (target, method)
{
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.