Search

Friday, November 30, 2007

Newbie Question: How do I figure out the inheritance chain of a type

Redmond_2005_0417_010447

I got this question from someone just starting out on the .NET platform. He is used to hit F12 (go to definition) on types and then figure out the inheritance chain. However, he couldn't do that say on a number (int) or array.

The solution is to write a simple recursive routine as follows

static void Dump(Type t)
{
if (t != null)
{
Dump(t.BaseType);
Console.WriteLine(t.ToString());
}
}

The routine can be called as follows

Dump(typeof(int));
Dump("abc".GetType());
Dump(1.GetType());
Dump(typeof(EventAttributes));
No marks for guessing the output though :)

No comments: