Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print out all the base type of an object?

Tags:

c#

.net

I have an object, I would like to print out all its parent type up to the Object? How to do that?

like image 325
user705414 Avatar asked Jan 19 '12 15:01

user705414


2 Answers

If you're only interested in the class hierarchy:

Type type = obj.GetType();
while (type != null)
{
    Console.WriteLine(type.Name);
    type = type.BaseType;
}
like image 125
Jon Skeet Avatar answered Nov 19 '22 20:11

Jon Skeet


var t = obj.GetType();

while (t != null)
{
    Console.WriteLine(t.Name);
    t = t.BaseType;
}
like image 2
Henk Holterman Avatar answered Nov 19 '22 21:11

Henk Holterman