public void DumpObject(string name, object value)
{
if (value == null)
{
// Null values
DumpText(name, "NULL");
}
else
{
Type type = value.GetType();
if (type.IsPrimitive || type.IsEnum)
{
// Primitive value - single line
DumpText(name, value.ToString());
}
else if (type == typeof(string))
{
// String - single line, quotes
DumpText(name, "'{0}'", value);
}
else
{
var formProxy = value as FormProxy;
var dumpable = value as IDumpabled;
if (dumpable != null)
{
// Call custom method of custom dumpable objects
dumpable.Dump(name, this);
}
else if (formProxy != null && currentDepth > 0)
{
// Print just the form if reference
DumpText(name, "{0}", formProxy);
}
else
{
if (formProxy != null)
{
// Print the content of the root form only
DumpText(name, "{0} {{", formProxy);
}
else
{
// Print the content of an arbitrary object
DumpText(name, "{");
}
if (Enter())
{
bool anyOutput = false;
// Get all public instance properties (skip inherited and indexers)
foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.DeclaringType == type && p.GetIndexParameters().Length == 0).OrderBy(p => p.Name))
{
object val = property.GetValue(value, null);
DumpObject(property.Name, val);
anyOutput = true;
}
// Print enumerable items
var asEnumerable = value as IEnumerable;
if (asEnumerable != null && asEnumerable.GetEnumerator().MoveNext())
{
DumpText("[");
if (Enter())
{
foreach (var obj in (IEnumerable)value)
{
DumpObject(obj);
}
Leave();
}
DumpText("]");
anyOutput = true;
}
if (!anyOutput && CurrentDepth >= 2)
{
// ToString if no properties and not an enumerable, unless current depth < 2
DumpText(value.ToString());
}
Leave();
}
DumpText("}");
}
}
}
}