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