23.4 Examining Methods Within a Type

 <  Day Day Up  >  

23.4 Examining Methods Within a Type

You want to view information about all the methods and their associated parameters within a type.


Technique

To enumerate each defined method within a Type , call the GetMethods method, which returns an array of MethodInfo objects. A method can contain 0 or more parameters, which you access through the GetParameters method defined in the MethodInfo class. Listing 23.2 enumerates all the methods of a given type and in turn enumerates all the parameters of that method by using the techniques just described. The final result for the assembly-viewer application is a list of methods and their parameters, which constitute individual nodes underneath a Methods node of the TreeView control.

Listing 23.2 Enumerating Methods and Their Parameters
 // get methods curType = new TreeNode( "Methods" ); foreach( MethodInfo method in t.GetMethods() ) {     string methodString = method.Name + "( ";     int count = method.GetParameters().Length;     foreach( ParameterInfo param in method.GetParameters() )     {         methodString += param.ParameterType;         if( param.Position < count-1 )             methodString += ", ";     }     methodString += " )";     curMember = new TreeNode( methodString );     curMember.Tag = method;     curType.Nodes.Add( curMember ); } newNode.Nodes.Add( curType ); // continued... 

Comments

Until this point, you should see a pattern emerging based on the application program interface (API) of the reflection classes. The design of these classes centers on logical method naming. For instance, if you want to get an array of parameters, you logically call the GetParameters method, and because parameters are contained within methods, you can deduce that the GetParameters method is defined in any member of a Type that contains parameters such as the MethodInfo and EventInfo member types. One thing that wasn't shown is the ability to further investigate a method by accessing various properties. For instance, IsStatic lets you know whether the method is a static method, and IsVirtual naturally specifies whether a method is virtual. The MethodInfo contains several Is Xxxx methods, allowing you to fully investigate every possible attribute associated with a method.

 <  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