#375 – Using GetInvocationList to Get a List of Individual Delegates

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.

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: