< Day Day Up > |
TechniqueThe generated delegate class is derived from System.MulticastDelegate , which means you are free to use any methods defined in that class for your delegate. To bind to multiple implementations , use the static method Combine , either passing an array of instantiated delegates bound to methods or passing two delegates you want to bind together to create a new delegate: static void Main(string[] args) { // create delegate array StringPrinterDelegate.PrintString[] printDelegates = new StringPrinterDelegate.PrintString[3]; // main delegate StringPrinterDelegate.PrintString stringPrinters; // create 3 delegates with 3 different implementations printDelegates[0] = new StringPrinterDelegate.PrintString( NormalPrint ); printDelegates[1] = new StringPrinterDelegate.PrintString( FirstCharPrint ); printDelegates[2] = new StringPrinterDelegate.PrintString( LastCharPrint ); // combine the 3 delegates into the main delegate stringPrinters = (StringPrinterDelegate.PrintString) Delegate.Combine( printDelegates ); // get string from user Console.Write( "Enter a string: " ); string input = Console.ReadLine(); // print with main delegate which causes all 3 to be invoked StringPrinterDelegate.PrintWithDelegate( stringPrinters, input ); } CommentsSometimes, you might want a single delegate to bind to several different implementations, thereby causing each implementation to be invoked when the delegate is called. This premise is behind the multicast delegate. A multicast delegate allows you to create several delegate implementations and bind them to only one delegate. Therefore, when the delegate is called, each method within the delegate's invocation list is called one after the other. In the code listing earlier, you can see a collection of PrintString delegates being created and placed into an array. Each of them are bound to a certain delegate implementation whose purpose is to simply print a string in a certain way. Once the collection is created, the static method Combine in the System.Delegate class is called, passing the collection as the only parameter. This method enumerates the entire collection and creates a multicast delegate. Because the generated delegate class derives from System.MulticastDelegate , you can assign a variable of that type from the result of the Combine method. However, because Combine returns a generic multicast delegate object, you have to cast the return value to the type you declared. Now any time the multicast delegate is called, each method that it is bound to is called. The PrintWithDelegate method is the same method shown in the previous recipe. No special programming is needed because the Invoke method that is generated for the delegate class will enumerate each method. One thing to keep in mind when creating multicast delegates is order. Unless you are calling a delegate asynchronously, which is discussed in a later recipe, order can be important. The order of method invocations on a multicast delegate is determined based on the order in which the method implementations were bound to it. For the example earlier, the first method invoked is the lowest indexed method within the collection used to create the multicast delegate. |
< Day Day Up > |