Can reflection tell the base class of an object?

S

stktung

Hey guys,

Are there functions in reflection that gives information about the base
classes of an object?

Thanks in advance
-Steve
 
M

Mythran

Hey guys,

Are there functions in reflection that gives information about the base
classes of an object?

Thanks in advance
-Steve

Here's an example:

Private Sub ShowInheritanceTree(ByVal Instance As Object)
Dim t As Type = Instance.GetType()
Dim n As Integer = 0
Dim tmp As Type = t
Dim names As ArrayList = New ArrayList()

While Not tmp Is GetType(Object)
If tmp Is Nothing
tmp = t.BaseType
Else
tmp = tmp.BaseType
End If

names.Add(tmp.Name)
n += 1
End While

Dim line As String
Dim spaces As String
For i As Integer = n - 1 To 0 Step -1
line = String.Empty
If i < n - 1
spaces = StrDup(((n - 1) - i) * 4, " ")
line = spaces & "|" & vbNewLine & spaces & "--- "
End If

line &= CStr(names(i))
Console.WriteLine(line)
Next i
End Sub

This method goes up the inheritance tree looking for the base types of each
class the current class inherits from then reverses the search to display
each class from top-to-bottom.

HTH,
Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top