Event or delegate gets multiple return values and exception handling

Source: Internet
Author: User

Theoretical knowledge:

1. The delegate generates a class that inherits from MulticastDelegate after compiling, and this multicastdelegate inherits from delegate. Within delegate, a list of delegates is maintained, and each element on the list is a delegate object that contains only one target method. By delegate the Getinvocationlist () static method of the base class, you can obtain this delegate list. The linked list is then facilitated by invoking the method with each delegate object in the list, so that the return value of each method can be obtained separately.

2. Delegate the DynamicInvoke () method in the base class:

public Object DynamicInvoke (params object[] args)

This is the most common method of invoking a delegate, and applies to all types of delegates. It takes a parameter of params object[], which means that it can take any type as an argument and return a single Object object.

The following is a generic way to invoke a method in a delegate's linked list:

1             Private Staticlist<Object> FireEvent (Delegate del,params Object[] args)2             {3list<Object> objlst =Newlist<Object>();4                 if(Del! =NULL)5                 {6Delegate[] Delegates =del. Getinvocationlist ();7                     foreach(Delegate methodinchdelegates)8                     {9                         TryTen                         { One                             Objectobj =method. DynamicInvoke (args); A                             if(obj! =NULL) -                             { - objlst.add (obj); the                             } -                         } -                         Catch(Exception e) -                         { +  -                         } +  A                     } at                 } -  -                 returnObjlst; -}

Event or delegate gets multiple return values and exception handling

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.