23.6 Displaying Property Information

 <  Day Day Up  >  

You want to inspect individual properties in a given type definition.


Technique

The GetProperties method defined in the Type class returns an array of PropertyInfo objects. These objects represent the list of available properties and their associated types that an object supports. Properties can support a get or set method or both for client access. To determine which methods a given property supports, use the CanRead or CanWrite properties defined in the PropertyInfo class. Based on the values of these properties, you can inspect the corresponding methods using the technique from Recipe 23.4, "Examining Methods Within a Type," to view the property's get and set methods. To obtain an array of MethodInfo objects, call the GetAccessors method, or to individually retrieve a single get or set method, you can call the GetGetMethod or GetSetMethod methods.

A property can also be defined as an indexer for a class. This indexer contains parameters that a client uses within brackets to retrieve information from the type. Recipe 23.4 demonstrated how to access the ParameterInfo object for a given method, and you use the same process for indexer properties. By calling the GetIndexParameters , you can enumerate a ParameterInfo array to retrieve each parameter declared for an indexer.

The code for this recipe represents the final portion of the assembly-viewer application that was built sequentially in the last recipes. The application uses a TreeView control to display assembly information in hierarchical form and a PropertyGrid to display individual information for each node as it is clicked. This information is stored within the Tag property for each node and is assigned to the PropertyGrid whenever you click a new node within the TreeView , as shown in the tvAssembly_AfterSelect method.

Listing 23.3 Enumerating Class Properties
 // get properties     curType = new TreeNode( "Properties" );     foreach( PropertyInfo property in t.GetProperties() )     {         curMember = new TreeNode( property.Name );         curMember.Tag = property;         curType.Nodes.Add( curMember );     }     newNode.Nodes.Add( curType );     parent.Nodes.Add( newNode ); } private void tvAssembly_AfterSelect(object sender,     System.Windows.Forms.TreeViewEventArgs e) {     if( e.Node.Tag != null )     {         pgObject.SelectedObject = e.Node.Tag;     }     else     {         pgObject.SelectedObject = null;     } } 

Comments

Properties utilize a special syntax, which is later translated into methods when the Microsoft Intermediate Language (MSIL) code is generated. This method of property implementation is likely a successor to the methods employed in the Active Template Library (ATL) within Visual C++. However, whereas a developer in ATL has to manually create the get and set methods for property implementation, C# uses a special syntactical construct to mentally distinguish a property from a method. The sample code for this recipe lists properties defined in a specified Type . However, if you compare the results with that of the MethodInfo enumeration, you see where the two overlap. For instance, a class deriving from System.Windows.Forms.Form contains a property named BackColor . If you view the list of MethodInfo objects, you also see two methods named get_BackColor and set_BackColor . Depending on your application design, you might not want this duplication, which means you have to filter out all property getter and setter methods, a technique explained in Recipe 23.8, "Searching Assemblies Using Custom Search Techniques."

 <  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