VB.Net - A Debugging Gift for Objects

GregH

23-01-2006 10:09:51

Hi Folks,

If you're savvy with System.Reflection please ignore this post.

I wanted to give something back to the Community.

This may or may not be useful, but I've spent a reasonable amount of time playing with System.Reflection as a debugging tool. This was motivated by the fact that I wanted to see *all* of a specified object or class's properties and their current values without having to write buckets of code. I wanted it in the Output Window (or a log file) and I wanted it now. And I wanted to call it with one line of code.

The following Sub can go anywhere as long as you Import System.Reflection and any other relevant libraries (eg: OgreDotNet) and also note you should change the parameter line to read the specific type of object you wish to interrogate...in this example it's a Camera...but it can be used in any VB.Net app with any object, and I usually don't pass an object to it as I use it within a class with Me.GetType instead of pObject.GetType so a class becomes self reporting (in a half-shaded ListView object conjured on a form from within the class - very sexy).




Imports System.Reflection


Module ObjectDebug

Public Sub ReportProperties(ByVal pObject As Camera)

'This subroutine uses reflection to interogate the properties
'of this class...

Dim PInfo As PropertyInfo
Dim Spaces As Integer
Dim MaxProperyNameLength As Integer

Debug.WriteLine(vbCr & "--- Property Report for " & pObject.GetType.ToString & " ---" & vbCr)

'Determine the longest property name (for formatting purposes)...

For Each PInfo In pObject.GetType.GetProperties()

If PInfo.Name.Length > MaxProperyNameLength Then
MaxProperyNameLength = PInfo.Name.Length
End If

Next PInfo

'By this stage, we know the length (in chars) of the
'longest property name.

For Each PInfo In pObject.GetType.GetProperties()

Try

'Determine the details for this property..

Spaces = (MaxProperyNameLength - PInfo.Name.Length) + 1

Debug.WriteLine("Property " & PInfo.Name & Space(Spaces) & _
"Value: " & PInfo.GetValue(pObject, Nothing).ToString)


Catch ex As Exception

Debug.WriteLine("Property " & PInfo.Name & Space(Spaces) & _
"Value: Unknown")

End Try

Next PInfo


Debug.WriteLine(vbCr & "--- End of Property Report for " & pObject.GetType.ToString & " ---" & vbCr)


End Sub 'ReportProperties

End Module



...and just for the sake of absolute clarity/context (which most of us are guilty of forgetting)... you use it like this in your calling app...


...
...

Dim MyCamera As Camera = MySceneManager.CreateCamera("MainCam")

ReportProperties(MyCamera)

...
...


Now look in the Visual Studio Output Window and hey presto the object reveals its inner most secrets...except for properties that are shy (aka "Unknown") ...and while I'm on that subject - why on earth is the Camera.LookAt property a writeonly property ????????????? One might want to know what vector the camera is looking at...and I suspect this to be an **issue** in my current investigation about grumpy cameras. Hmmm..might need to use the AutoTrack instead methinks.

I hope you find it useful...I'm about to interrogate the aforesaid grumpy camera to find out why it's breaking my app. Oh..and keep this in mind...I'm going to compare the debug output of a working camera against this SOB's output. One line spew. Way to go !!!

BTW, If you find this useful, please attach a reply because I'm new to the whole forum environment and need to know if I'm doing the right thing, or just telling you stuff you already knew. :)

Cheers,

G.

EagleEye

23-01-2006 14:59:37

I have a question...

Public Sub ReportProperties(ByVal pObject As Camera)

Why would you specify "As Camera" instead of "As Object"? You're kinda limiting yourself there aren't you?

GregH

24-01-2006 01:51:50

Hi EagleEye,

You are quite right - VB.Net will cast correctly if you use the generic Object as the parameter:



ReportProperties(pObject as Object)



...I didn't think to test/use the generic object at the time because it was late and I was dealing with a specific camera issue (hence I specified camera in the parameter line). Yup, by all means the code above will work with whatever you throw at it !!!

Cheers,

G

EagleEye

24-01-2006 05:27:45

I figured Object would work... just wanted to make sure the "Camera" thing was an oversight.

GregH

24-01-2006 08:31:55

Hi EagleEye,

In a 3D World, you can have oversight, undersight everywhich-way sight and even blind sight. Flat is easy !!!

God I've gotta get some sleep...

Cheers,

Greg H.