By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

Can reflection tell the base class of an object?

P: n/a
Hey guys,

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

Thanks in advance
-Steve

Nov 3 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Check out the Type class's GetBaseType method.

http://msdn2.microsoft.com/en-us/lib...e_members.aspx

--
Mike

Mike McIntyre [MVP]
http://www.getdotnetcode.com
<st*****@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hey guys,

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

Thanks in advance
-Steve

Nov 3 '06 #2

P: n/a

<st*****@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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
Nov 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.