The delegation is causing trouble again-Remember to cancel the registration of the delegation chain and obtain the return value.

Source: Internet
Author: User

Today, I encountered a problem when I changed the bug. Multiple methods were registered to an event, and these methods were mutually exclusive. As a result, I found a bug, I was so depressed that I reviewed my previous knowledge and made some proper extensions. I recorded and shared my achievements to prevent them from being committed again.
Obviously, this is a delegated chain. First of all, let's review the initial Writing Method of the delegated chain. Here we reference N writing methods of the previous delegate. Which one do you like? .

Define delegation:

  PlusStringHandle( x,  y);

          plusString( x,  x +

 

            PlusStringHandle handle = +=+= str = handle(, , handle.Method.Name);

TestString (x, x + y +

            PlusStringHandle handle = +=+=+=-=-= str = handle(, , handle.Method.Name);

Continue to modify the Main method:

            PlusStringHandle handle = +=+=+=            handle = testString;               str = handle(, , handle.Method.Name);

=================================================== ==================

 

Next, we will continue to discuss the returned values obtained by the delegate chain. As described above, directly calling the delegate can only obtain the returned values of the last registered method, there are two methods to obtain the returned values of all registered methods:

            PlusStringHandle handle = +=+=+= str = handle(, = (PlusStringHandle d , d.Method.Name, d(,  (Delegate d                 Console.WriteLine(, d.Method.Name, d.DynamicInvoke(, 

  

Related Article

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.