#375 – Using GetInvocationList to Get a List of Individual Delegates
July 26, 2011 Leave a comment
Delegates defined in C# using the delegate keyword are multicast delegates, meaning that they can hold zero or more references to methods.
In the example below, we define a delegate type, create a delegate instance and then point it to two methods.
private delegate void StringHandlerDelegate(string s); static void Main() { // Add two methods to delegate instance StringHandlerDelegate del = Method1; del += Method2; }
At this point, if we invoked the delegate, both methods would be called.
You can use the GetInvocationList method of System.MulticastDelegate to get a list of individual delegates, each of which represents a single method.
Delegate[] dels = del.GetInvocationList();
Notice that each element in the array, while only representing a single method to be called, is itself an instance of the original multicast delegate, i.e. StringHandlerDelegate.