I'm trying to build a property inspector method that iteratively inspects all an objects properties and recursively examines all of the sub-properties of those properties until it either runs out of properties or reaches the max recursion level/depth.
However I am running into an issue in tracking the recursion depth due to the fact that the recursion takes place inside of a iterative loop, therefore each property of an object increments the recursion depth, when it shouldn't, only the next level down should.
Here is my example code:
Dim MaxRecursionLevel As Integer = 10
Dim CurrentRecursionLevel As Integer = 1
Function GetPropertiesWithName(objToSearch as Object, optional nameFilters as String()
dim result as new List(of Object)
If CurrentRecursionLevel > MaxRecursionLevel Then Exit Function
CurrentRecursionLevel = CurrentRecursionLevel + 1
'Iteration of properties
For Each item As PropertyInfo In objToSearch.GetType.GetProperties()
if PropertyHasName(nameFilters) Then
result.Add(item)
dim subItem as Object = item.GetValue(objToSearch, Nothing)
'Recursion of Subproperties
dim subResult as List(of Object) = GetPropertiesWithName(subItem, nameFilters)
if not subResult is Nothing then result.Addrange(subResult)
End If
Next
Return result
End Function
How can I accurately track the recursion depth, or is there a better way to go about this?