The mechanism principle of delegate in C #

Source: Internet
Author: User
Tags instance method
The keyword delegate in C # actually tells the compiler to generate a sealed class that inherits from MulticastDelegate. The primary responsibility of the class is to create a callback environment for the function.
The resulting sealed class is defined as follows:
Namespace definenamespace{public

delegate String Definedelegate (bool A, bool B, bool c);

public class Otherclassone
{public
     definedelegate mydelegate;
}

public class otherclasstwo{}
}

The compiler will actually generate:
Namespace definenamespace{public

sealed class DefineDelegate:System.MulticastDelegate
{  public String  Invoke (bool A, bool B, bool c);
     Public  IAsyncResult BeginInvoke (bool A, bool B, BOOL C, AsyncCallback CB, Object state);
     Public  string Endinvoce (IAsyncResult result);
}

public class Otherclassone
{public
     definedelegate mydelegate;
}

public class otherclasstwo{}
}



In addition, the MulticastDelegate class and the delegate class are defined as follows:
Public abstract class Multicastdelegate:delegate
{
      //List of methods added to delegates public
      sealed override delegate[] Getinvactionlist ();
     
      Overloaded operator public
      static bool operator = = (MulticastDelegate d1, MulticastDelegate D2);
      public static bool operator!= (multicastdelegate d1, MulticastDelegate D2);
    
      A list of methods maintained by the internal administration delegate
     private IntPtr _invocationcount;
     Private object _invocationlist;
}

The delegate class is defined as follows:
Public abstract class Delegate:icloneable, iserializeable
{
       //method of interacting with function list
       ... Combine ...
       ... Remove ...
       ... RemoveAll ...

    //extends the properties of the delegate target public
    MethodInfo method {get;} Reflection, the information of the static method or instance method maintained by the delegate is public
    object Target {get;} Only the delegate's method definition is not NULL when the class instance is represented, the object that represents the delegate method.
}


Description of delegate Definedelegate: Declaring a delegate is actually creating a class that inherits from MulticastDelegate. To use a delegate, you must have a delegate instance or delegate object of that delegate class on top of a class. For example Otherclassone in the example definition: public definedelegate mydelegate; Invoke to call the method synchronously. The return value and argument list are consistent when defined with the delegate keyword. Because a delegate object can maintain a list of delegate methods and add it through the operator = =, invoke calls the list of methods added to the delegate instance in turn. BeginInvoke and EndInvoke are a pair of operations that are used when an asynchronous call is made to a delegate.
Description of the asynchronous invocation of the delegate definedelegate:
When a BeginInvoke method of a delegate object is invoked, a worker thread is created for the object and the list of methods maintained by the delegate object is invoked within that thread. The keynote thread code should create a asynccallback delegate object to pass to BeginInvoke. As shown in the following code:
  private void SomeMethod ()
         {
              ...
               AsyncCallback DELEGATECB = new AsyncCallback (this.invokecomplete);
              String completeinfo = "Delegate async handle over";
              Mydelegate.begininvoke (Paramslist ..., DELEGATECB, completeinfo);//will be processed in another thread ...
         }

        private void Invokecomplete (IAsyncResult result)
       {
             asyncresult ar = (asyncresult) result;
             Definedelegate tmpdelegate = (definedelegate) ar. AsyncDelegate;
             String delegateresult = Tmpdelegate.endinvoke (Result),//Gets the processing results of the delegate method
             string completeinfo = (string). asyncstate;//the state parameter for incoming BeginInvoke
       }



















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.