How to Implement C # proxy (delegate)

Source: Internet
Author: User
      C # proxy is actually similar to the function pointer in C ++. This article describes how to implement a C # proxy in three steps.

      The C # proxy is actually similar to the function pointer in C ++. Because C # does not have a pointer, you can use the proxy to complete some operations that were originally completed using the function pointer in C ++, for example, to pass the M method of Class A to the object of Class B, so that the object of Class B can call this m method. But compared with function pointers, delegate has many advantages that function pointers do not possess. First, the function pointer can only point to static functions, while delegate can reference both static functions and non-static member functions. When referencing a non-static member function, delegate not only saves the reference to this function entry pointer, but also saves the reference to the class instance that calls this function. Second, compared with function pointers, delegate is an object-oriented, secure, and reliable managed object. That is to say, the runtime can ensure that the delegate points to a valid method. You do not need to worry that the delegate will point to an invalid or out-of-bounds address.

      It is very easy to implement a C # proxy. You can implement a delegate through the following three steps:

      1. Declare a delegate object. It should have the same parameter and return value type as the method you want to pass.

      Example of declaring a proxy:

 
  
  
  1. Public Delegate int mydelegate (string message );

2. Create a delegate object and pass in the functions you want to pass as parameters.

How to Create a proxy object:

1). mydelegate = new mydelegate (Instance name. Method Name );

2). mydelegate = new mydelegate (class name. Method Name );

Note: If the proxy method is a static method, 2nd methods are used; otherwise, 1st methods are used.

3. Use the object created in the previous step to call the method where an asynchronous call is to be implemented.

You can directly use the proxy to call the method pointed to by the Proxy:

Mydelegate (parameters passed to the method );

Note the following:

"Proxy" (delegate): "proxy" is type-safe and fully object-oriented.

(1) in C #, all proxies are derived from the system. Delegate class (delegate is the alias of system. Delegate ).

(2) The proxy has an implicit sealed attribute, that is, it cannot be used to derive a new type.

(3) The biggest role of the proxy is to bind events of the class for processing.Program.

(4) Before calling a function through a proxy, you must first check whether the proxy is null. If it is not null, you can call the function. (5) static methods or instance methods can be encapsulated in the proxy instance.

(6) When creating a proxy instance, you must pass the method to be mapped or other proxy instance to specify the prototype of the function to be encapsulated by the proxy (. NET is called the method Signature: signature ). Note: If the ing is a static method, the passed parameter should be the class name. method name. If the ing is an instance method, the passed parameter should be the Instance name. method name.

(7) only when the methods mapped to the two proxy instances and the objects to which the method belongs are the same can they be considered to be equal (considering the function address ).

(8) multiple proxy instances can form a proxy chain, system. the delegate defines the static methods combion and remove used to maintain the proxy chain, respectively adding the proxy instance to the proxy chain and deleting the proxy instance.

(9) The definition of the proxy must be placed outside any class, such as delegate int mydelegate (). In the class method, call mydelegate d = new mydelegate (myclass. mymethod); to instantiate the instance of the custom proxy.

(10) agent three steps:

A. Generate a custom proxy class: Delegate int mydelegate ();

B. Then instantiate the proxy class: mydelegate d = new mydelegate (myclass. mymethod );

C. Finally, call the method through the Instance Object: int ret = D (); 0 0 0

In this way, the C # proxy is implemented.

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.