Created
May 8, 2015 16:00
-
-
Save christherama/64fd594160994b770de8 to your computer and use it in GitHub Desktop.
Procedures for displaying an object in a well-formatted manner. This is helpful for debugging in the console.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' <summary> | |
''' Displays an object's properties in a well-formatted manner. | |
''' </summary> | |
''' <param name="obj">Object to display</param> | |
''' <param name="numTabs">Number of tabs to prefix the object's properties with</param> | |
Public Shared Sub DisplayObject(ByVal obj As Object, Optional ByVal numTabs As Integer = -1) | |
' Define non-special types | |
Dim types As String() = {"String", "Boolean", "DateTime", "Integer", "Double", "Long"} | |
' Get the properties of this object | |
Dim properties As PropertyInfo() = obj.GetType().GetProperties() | |
For Each prop In properties | |
DisplayTabs(numTabs) | |
Console.Write(prop.Name & ": ") | |
Dim value As Object = prop.GetValue(obj, New Object() {}) | |
If IsArray(value) Then | |
Console.WriteLine("[") | |
Dim items As Object() = DirectCast(value, Object()) | |
For Each item As Object In items | |
DisplayTabs(numTabs + 1) | |
Console.WriteLine("{") | |
DisplayObject(item, numTabs + 2) | |
DisplayTabs(numTabs + 1) | |
Console.WriteLine("}") | |
Next | |
DisplayTabs(numTabs) | |
Console.WriteLine("]") | |
ElseIf Array.IndexOf(types, value.GetType().Name) < 0 Then | |
Console.WriteLine() | |
DisplayObject(value, numTabs + 1) | |
Else | |
Console.WriteLine(value) | |
End If | |
Next | |
End Sub | |
''' <summary> | |
''' Helper procedure to display a certain number of tabs (2 spaces) | |
''' </summary> | |
''' <param name="numTabs">Number of tabs to display</param> | |
Private Shared Sub DisplayTabs(ByVal numTabs As Integer) | |
For i As Integer = 0 To numTabs | |
Console.Write(" ") | |
Next | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment