📜  C# 打印对象的所有属性,包括子对象 - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:39.395000             🧑  作者: Mango

代码示例1
public void DisplayObject(object obj)
{
    var type = obj.GetType();
    foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(type))
    {
        if (descriptor.PropertyType.IsClass 
            && descriptor.PropertyType.Assembly.FullName == type.Assembly.FullName)
        {
            var value = descriptor.GetValue(obj);
            DisplayObject(value);
        }
        else
        {
            string name = descriptor.Name;
            object value=descriptor.GetValue(obj);
            Console.WriteLine("{0}={1}",name,value);
        }
    }
}