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);
}
}
}
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);
}
}
}