~ubuntu-branches/ubuntu/quantal/ipod-sharp/quantal

« back to all changes in this revision

Viewing changes to src/ObjectDumper.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-11-13 12:36:39 UTC
  • mfrom: (1.1.4 upstream) (6.1.5 edgy)
  • Revision ID: james.westby@ubuntu.com-20061113123639-cynm2axegef1etff
* New upstream release
* debian/patches/01_fix-playcounts.patch:
  + Dropped, merged upstream
* debian/control:
  + Build depend on mono-gmcs, libmono-sharpzip2.84-cil, libmono2.0-cil,
    libglib2.0-dev and libipoddevice >= 0.5.0
  + Bumped Standards-Version to 3.7.2
  + Add ${misc:Depends} to Depends
  + Let libipodui-cil depend on libipod-cil
* debian/libipod-cil.install:
  + Add ipod-sharp-firmware.dll
* debian/patches/01_dllmap.patch:
  + Add dllmap to libgobject-2.0.so.0 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
using System;
 
3
using System.Collections;
 
4
using System.Reflection;
 
5
 
 
6
namespace IPod {
 
7
 
 
8
    internal class ObjectDumper {
 
9
 
 
10
        public static void Dump (object o) {
 
11
            Dump (o, 0, new ArrayList ());
 
12
        }
 
13
 
 
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);
 
17
        }
 
18
 
 
19
        private static void Dump (object o, int level, ArrayList previous) {
 
20
            Type type = null;
 
21
 
 
22
            if (o != null) {
 
23
                type = o.GetType ();
 
24
            }
 
25
            
 
26
            Dump (o, type, null, level, previous);
 
27
        }
 
28
        
 
29
        private static void Dump (object o, Type type, string name, int level, ArrayList previous) {
 
30
            if (o == null) {
 
31
                Console.WriteLine (Pad (level, "{0} ({1}): (null)", name, type.Name));
 
32
                return;
 
33
            }
 
34
 
 
35
            if (previous.Contains (o)) {
 
36
                return;
 
37
            }
 
38
 
 
39
            previous.Add (o);
 
40
 
 
41
            if (type.IsPrimitive || o is string) {
 
42
                DumpPrimitive (o, type, name, level, previous);
 
43
            } else {
 
44
                DumpComposite (o, type, name, level, previous);
 
45
            }
 
46
        }
 
47
 
 
48
        private static void DumpPrimitive (object o, Type type, string name, int level, ArrayList previous) {
 
49
            if (name != null) {
 
50
                Console.WriteLine (Pad (level, "{0} ({1}): {2}", name, type.Name, o));
 
51
            } else {
 
52
                Console.WriteLine (Pad (level, "({0}) {1}", type.Name, o));
 
53
            }
 
54
        }
 
55
 
 
56
        private static void DumpComposite (object o, Type type, string name, int level, ArrayList previous) {
 
57
 
 
58
            if (name != null) {
 
59
                Console.WriteLine (Pad (level, "{0} ({1}):", name, type.Name));
 
60
            } else {
 
61
                Console.WriteLine (Pad (level, "({0})", type.Name));
 
62
            }
 
63
 
 
64
            if (o is IDictionary) {
 
65
                DumpDictionary ((IDictionary) o, level, previous);
 
66
            } else if (o is ICollection) {
 
67
                DumpCollection ((ICollection) o, level, previous);
 
68
            } else {
 
69
                MemberInfo[] members = o.GetType ().GetMembers (BindingFlags.Instance | BindingFlags.Public |
 
70
                                                                BindingFlags.NonPublic);
 
71
                
 
72
                foreach (MemberInfo member in members) {
 
73
                    try {
 
74
                        DumpMember (o, member, level, previous);
 
75
                    } catch {}
 
76
                }
 
77
            }
 
78
        }
 
79
 
 
80
        private static void DumpCollection (ICollection collection, int level, ArrayList previous) {
 
81
            foreach (object child in collection) {
 
82
                Dump (child, level + 1, previous);
 
83
            }
 
84
        }
 
85
 
 
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));
 
89
 
 
90
                Dump (dictionary[key], level + 2, previous);
 
91
            }
 
92
        }
 
93
 
 
94
        private static void DumpMember (object o, MemberInfo member, int level, ArrayList previous) {
 
95
            if (member is MethodInfo || member is ConstructorInfo ||
 
96
                member is EventInfo)
 
97
                return;
 
98
 
 
99
            if (member is FieldInfo) {
 
100
                FieldInfo field = (FieldInfo) member;
 
101
 
 
102
                string name = member.Name;
 
103
                if ((field.Attributes & FieldAttributes.Public) == 0) {
 
104
                    name = "#" + name;
 
105
                }
 
106
                
 
107
                Dump (field.GetValue (o), field.FieldType, name, level + 1, previous);
 
108
            } else if (member is PropertyInfo) {
 
109
                PropertyInfo prop = (PropertyInfo) member;
 
110
 
 
111
                if (prop.GetIndexParameters ().Length == 0 && prop.CanRead) {
 
112
                    string name = member.Name;
 
113
                    MethodInfo getter = prop.GetGetMethod ();
 
114
 
 
115
                    if ((getter.Attributes & MethodAttributes.Public) == 0) {
 
116
                        name = "#" + name;
 
117
                    }
 
118
                    
 
119
                    Dump (prop.GetValue (o, null), prop.PropertyType, name, level + 1, previous);
 
120
                }
 
121
            }
 
122
        }
 
123
    }
 
124
}