user705414
user705414

Reputation: 21200

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

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

Upvotes: 10

Views: 11142

Answers (3)

Henk Holterman
Henk Holterman

Reputation: 273314

var t = obj.GetType();

while (t != null)
{
    Console.WriteLine(t.Name);
    t = t.BaseType;
}

Upvotes: 2

vc 74
vc 74

Reputation: 38179

Type currentType = obj.GetType();
while (currentType != null)
{
  Console.WriteLine(currentType.ToString());
  currentType = currentType.BaseType;
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1501626

If you're only interested in the class hierarchy:

Type type = obj.GetType();
while (type != null)
{
    Console.WriteLine(type.Name);
    type = type.BaseType;
}

Upvotes: 8

Related Questions