Reputation: 21200
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
Reputation: 273314
var t = obj.GetType();
while (t != null)
{
Console.WriteLine(t.Name);
t = t.BaseType;
}
Upvotes: 2
Reputation: 38179
Type currentType = obj.GetType();
while (currentType != null)
{
Console.WriteLine(currentType.ToString());
currentType = currentType.BaseType;
}
Upvotes: 1
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