如何打印出对象的所有基本类型?

时间:2012-01-19 15:04:34

标签: c# .net

我有一个对象,我想打印出它的所有父类型到对象?怎么做?

3 个答案:

答案 0 :(得分:7)

如果您只对 class 层次结构感兴趣:

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

答案 1 :(得分:2)

var t = obj.GetType();

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

答案 2 :(得分:1)

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