23.3 Examining Constructor Information

 <  Day Day Up  >  

You want to extract information about all constructors within a type defined in a module.


Technique

The previous two recipes looked at assemblies and modules. This recipe is the first of five that looks at the next rung in the ladder, types. If you look within the documentation and compare the number of parameters that the Type class has compared to the Module or Assembly , you'll see why we consider them the main workhorse within an assembly. Consider creating a class that represents declarations for classes, value types, arrays, interfaces, pointers, enumerations, and structs, and you'll know why so many properties exist within the Type class.

Each Type contains a collection of members. In Listing 23.1, you view any members that are marked as constructors by calling the GetConstructors method defined in the Type class. It returns a ConstructorInfo array, allowing you to view detailed properties specific to a constructor. The alternative to this method is to use the GetMembers method to enumerate all the members using a MemberInfo array. Either method works, although using GetMembers requires the extra steps of accessing the MemberType property of each MemberInfo and casting the MemberInfo object to the appropriate member type information class corresponding to the MemberType property. The following example continues the assembly-viewer tool by enumerating each Type object within a Module and proceeds to add any constructors to the TreeView control underneath a Constructors node as shown in Listing 23.1.

Listing 23.1 Enumerating and Examining Constructors
 private void AddModule( Module mod, TreeNode parent ) {     TreeNode newNode = new TreeNode( mod.Name );     newNode.Tag = mod;     parent.Nodes.Add( newNode );     foreach( Type t in mod.GetTypes() )     {         AddType( t, newNode );     } } private void AddType( Type t, TreeNode parent ) {     TreeNode newNode = new TreeNode( t.Name );     newNode.Tag = t;     TreeNode curType;     TreeNode curMember;     // note that GetMembers could have been called to enumerate     // all the members defined in this type.  However, this program     // will separate them out based on what they are     // get constructor information     curType = new TreeNode( "Constructors" );     foreach( ConstructorInfo constructor in t.GetConstructors() )     {         curMember = new TreeNode( constructor.Name );         curMember.Tag = constructor;         curType.Nodes.Add( curMember );     }     newNode.Nodes.Add( curType );     // continued... 

Comments

This recipe and the following four break up the investigation of a Type object. Type objects are really where the bulk of the functionality for each assembly is contained, with the previous levels of the hierarchy reserved for organizational tasks . This recipe looks at how to enumerate all the available constructors within a given type.

You'll see that a lot of the remaining recipes within this chapter utilize the Type class to perform various operations. The introduction for this chapter mentioned that integrating reflection into an application might be difficult because it might be hard to think of how you could use it. You can use the Type class and the associated reflection methods for several different things, some of which appear in later recipes. One possible use is automatic documentation generation. The Type class contains a BaseType property, which contains the inherited Type of the current object. Using this information and other methods, you can construct a class hierarchy automatically during runtime. If your application utilizes an object model for any reason, this tool is a valuable debugging tool that you can use.

 <  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