Monday, February 11, 2008

Get the Inheritance Hierarchy of a Type

Since last few days I was working on a tool, where it was needed to get the inheritance hierarchy of a type … I have figured out some code which gave me what I was looking for….

public void PrintTypeName(Type type)
{
Console.WriteLine(type.ToString());
if (type.BaseType != null)
{
PrintTypeName(type.BaseType);
}
}


We can call this method as mentioned below ….
PrintTypeName(typeof(int))
PrintTypeName("ABC".GetType())
PrintTypeName('A'.GetType())

No comments: