Cloning delegates

delegates implement the ICloneable interface so that a delegate can be copied. The copy will be a deep copy so that each copy can exist independently, and changes to one will not affect changes in the other. Listing 14.19 shows a simple illustration. The full source for this code is in the DelegateClone directory.

Listing 14.19. Cloning delegates
 DelegateCallback ad = new DelegateCallback(ACallback); DelegateCallback bd = new DelegateCallback(BCallback); DelegateCallback cd = new DelegateCallback(CCallback); DelegateCallback dd = ad; dd += bd; dd += cd; DelegateCallback ed = dd; Console.WriteLine("Original list"); Delegate [] da = dd.GetInvocationList(); foreach(Delegate d in da) { Console.WriteLine("{0} {1} ", d.Target, d.Method); ...

Get .NET Common Language Runtime Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.