3
using System.Collections;
4
using System.Reflection;
8
internal class ObjectDumper {
10
public static void Dump (object o) {
11
Dump (o, 0, new ArrayList ());
14
private static string Pad (int level, string msg, params object[] args) {
15
string val = String.Format (msg, args);
16
return val.PadLeft ((level * 4) + val.Length);
19
private static void Dump (object o, int level, ArrayList previous) {
26
Dump (o, type, null, level, previous);
29
private static void Dump (object o, Type type, string name, int level, ArrayList previous) {
31
Console.WriteLine (Pad (level, "{0} ({1}): (null)", name, type.Name));
35
if (previous.Contains (o)) {
41
if (type.IsPrimitive || o is string) {
42
DumpPrimitive (o, type, name, level, previous);
44
DumpComposite (o, type, name, level, previous);
48
private static void DumpPrimitive (object o, Type type, string name, int level, ArrayList previous) {
50
Console.WriteLine (Pad (level, "{0} ({1}): {2}", name, type.Name, o));
52
Console.WriteLine (Pad (level, "({0}) {1}", type.Name, o));
56
private static void DumpComposite (object o, Type type, string name, int level, ArrayList previous) {
59
Console.WriteLine (Pad (level, "{0} ({1}):", name, type.Name));
61
Console.WriteLine (Pad (level, "({0})", type.Name));
64
if (o is IDictionary) {
65
DumpDictionary ((IDictionary) o, level, previous);
66
} else if (o is ICollection) {
67
DumpCollection ((ICollection) o, level, previous);
69
MemberInfo[] members = o.GetType ().GetMembers (BindingFlags.Instance | BindingFlags.Public |
70
BindingFlags.NonPublic);
72
foreach (MemberInfo member in members) {
74
DumpMember (o, member, level, previous);
80
private static void DumpCollection (ICollection collection, int level, ArrayList previous) {
81
foreach (object child in collection) {
82
Dump (child, level + 1, previous);
86
private static void DumpDictionary (IDictionary dictionary, int level, ArrayList previous) {
87
foreach (object key in dictionary.Keys) {
88
Console.WriteLine (Pad (level + 1, "[{0}] ({1}):", key, key.GetType ().Name));
90
Dump (dictionary[key], level + 2, previous);
94
private static void DumpMember (object o, MemberInfo member, int level, ArrayList previous) {
95
if (member is MethodInfo || member is ConstructorInfo ||
99
if (member is FieldInfo) {
100
FieldInfo field = (FieldInfo) member;
102
string name = member.Name;
103
if ((field.Attributes & FieldAttributes.Public) == 0) {
107
Dump (field.GetValue (o), field.FieldType, name, level + 1, previous);
108
} else if (member is PropertyInfo) {
109
PropertyInfo prop = (PropertyInfo) member;
111
if (prop.GetIndexParameters ().Length == 0 && prop.CanRead) {
112
string name = member.Name;
113
MethodInfo getter = prop.GetGetMethod ();
115
if ((getter.Attributes & MethodAttributes.Public) == 0) {
119
Dump (prop.GetValue (o, null), prop.PropertyType, name, level + 1, previous);