我有一个对象,我想打印出它的所有父类型到对象?怎么做?
答案 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;
}