5.4 Accessing Delegate Names Within a Multicast Delegate

 <  Day Day Up  >  

You want to view which methods are attached to a multicast delegate.


Technique

To retrieve an array of delegates that are attached to a multicast delegate, call the GetInvocationList method from the multicast delegate instance. You can access the method name and additional method information on each delegate by using the Method property defined in the delegate. This step returns a MethodInfo object, and you find the name of the method bound to that delegate by accessing the Name property of that object:

 
 class StringPrinterDelegate {     public delegate void PrintString( string s );     // delegate passed as parameter     public static void PrintWithDelegate( PrintString psDelegate, string s )     {         Console.Write( "Invocation List Method Names: " );         // enumerate through each delegate in invocation list         foreach( Delegate dg in psDelegate.GetInvocationList() )             Console.Write( dg.Method.Name + " " );         Console.WriteLine();         // call delegate         psDelegate( s );     } } 

Comments

Whenever you start programming anything that is dynamic in nature, you lose any predefined knowledge about objects or methods or anything else that you normally know before you compile. In other words, when you want to call a method, you know what the method name is and you can use that information to output tracing or debug information. However, once you enter the dynamic realm where objects are attached during runtime, or the invocation of methods occurs dynamically based on the running state of the application, you don't always know what object is being accessed or what method is being called at any given point in time. That is, you don't know unless you have some sort of runtime type information or reflection capabilities built into the language or your application.

The .NET Framework is a very dynamic environment and as such has rich support for runtime type information and the ability to inspect an object and its methods at any point. The multicast delegate class is no exception. There are a few reasons why you would want to inspect the methods that a multicast delegate is bound with, the most probable reason being debugging.

You saw earlier how to use the GetInvocationList to access individual delegates within a multicast delegate so you can remove them from the invocation list. One thing that wasn't pointed out was that you can also enumerate this list to gather information about each delegate. The invocation list is simply an array of delegate objects whose type is System.Delegate . Within that class is a Method property that contains any information you need about the method to which the delegate is bound. Although this recipe singles out the Name property to determine the name of the bound method, there are other properties that you might find useful.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net