The dynamic invocation of Delegates is essentially dynamic invocation of methods. To explore this invocation on delegates, one needs to use multicast delegates. To brief about multicast delegates, this is a way to combine /compose delegates with the help of '+' (combine) and '-' (remove). This facilitates the sequential notifications in scenarios where a publish-subscribe/observer pattern would be implemented and in scenarios where assemblies and types are loaded dynamically.

When a subscriber (method) is added to a multicast delegate, the MulticastDelegate class creates a new instance of the delegate type, stores the object reference and the method pointer for the added method into the new instance, and adds the new delegate instance as the next item in a list of delegate instances. In a sense, a linked list of the delegate objects is maintained by the MulticastDelegate class.

Generally such delegates are invoked in order of their addition to multicast delegate but this behaviour can be overridden. Such invocation can lead to issues if any of the invoked method throws an exception or some data is returned through containing delegate.

For delegates that don't return void or have some data returned, it is advised to enumerate through the invocation list to receive the retuned value.

We would cover early bound invocation in addition to this dynamic invocation.

Read More...