📜  获取对象的所有属性,包括子 c# 代码示例

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

代码示例1
private void DisplayObject(object obj)
{
    var type = obj.GetType();
    foreach(var propertyInfo in type.GetProperties())
    {
        object value = propertyInfo.GetValue(obj, null);
        if(propertyInfo.PropertyType.IsGenericType && 
            propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
        {
            foreach(object o in (IEnumerable)value)
            {
                DisplayObject(o);
            }
        }
        else
        {
            Console.WriteLine(value);
        }
    }
}