Question Reflection when object contains other objects

kblair

Member
Joined
Feb 11, 2010
Messages
8
Programming Experience
1-3
I am trying to create an object browser similar to what VS2010 uses for the quick watch window. I can use reflection to iterate through the properties and display their names and values until I get to the point in an object where the property is an object of my own creation and not a system type. I can't seem to find the correct approach to be able to iterate the properties of these "sub objects".

Below is the just of what I am trying to do

VB.NET:
Public class myClass
  property myprop a string
  property myprop2 as double
  property myProp3 as myObject
  .
  .
  .
end class

Public Class myObject
  property myObjProp as string
  .
  .
End Class

sub ShowObject (this as myClass)

  dim strName as string
  dim strValue as string 

  For Each Prop As System.Reflection.PropertyInfo In This.GetType.GetProperties

    'works fine for system types (string, double, int32, ect)
    strName = prop.name
    strValue = prop.getvalue(this,nothing).tostring

    'display strName & strValue in a grid

    'if the prop.propertytype is not a system type then
    'iterate through the object properties i.e. this.myObject.myObjProp


  next

end sub

Thanks for you help,

Kent
 
Last edited:
To iterate through the properties of class types you do the same thing, prop.PropertyType returns the type, from which you can get its properties, and 'prop' value is the object instance for which you get the sub-property values.
The .Net types and your own types is not really different, a class is a class, you can for example check the types IsClass for starters.

.ToString is something you must beware of, this is an instance method and you will get a NullReferenceException if current value is Nothing. CStr function can be used as alternative if you don't care to check for Nothing.
 
Back
Top