-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectDumper.cs
More file actions
200 lines (187 loc) · 5.48 KB
/
ObjectDumper.cs
File metadata and controls
200 lines (187 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright © Microsoft Corporation. All Rights Reserved.
// This code released under the terms of the
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
//
//Copyright (C) Microsoft Corporation. All rights reserved.
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Diagnostics;
public class ObjectDumper
{
public static void Write(object o, int depth = 0, string fromMethod = null)
{
ObjectDumper dumper = new ObjectDumper(depth);
StackTrace st = new StackTrace();
StackFrame? sf = st.GetFrame(1);
if (fromMethod == null) {
fromMethod = sf.GetMethod().Name;
}
dumper.Write($"From method: {fromMethod}");
dumper.WriteLine();
Type? type = o?.GetType();
dumper.Write($"Diplaying object type: {o?.GetType().Name}");
if (type?.IsGenericType == true)
{
dumper.Write($"<{string.Join(", ", type.GetGenericArguments().Select(a => a.Name))}>");
}
dumper.WriteLine();
dumper.Write("Data:"); dumper.WriteLine();
dumper.WriteObject(null, o);
dumper.WriteLine();
}
TextWriter writer;
int pos;
int level;
int depth;
private ObjectDumper(int depth)
{
this.writer = Console.Out;
this.depth = depth;
}
private void Write(string s)
{
if (s != null)
{
writer.Write(s);
pos += s.Length;
}
}
private void WriteIndent()
{
for (int i = 0; i < level; i++) writer.Write(" ");
}
private void WriteLine()
{
writer.WriteLine();
pos = 0;
}
private void WriteTab()
{
Write(" ");
while (pos % 8 != 0) Write(" ");
}
private void WriteObject(string? prefix, object o)
{
if (o == null || o is ValueType || o is string)
{
WriteIndent();
Write(prefix);
WriteValue(o);
WriteLine();
}
else if (o is IEnumerable)
{
foreach (object element in (IEnumerable)o)
{
if (element is IEnumerable && !(element is string))
{
WriteIndent();
Write(prefix);
Write("...");
WriteLine();
if (level < depth)
{
level++;
WriteObject(prefix, element);
level--;
}
}
else
{
WriteObject(prefix, element);
}
}
}
else
{
MemberInfo[] members = o.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance);
WriteIndent();
Write(prefix);
bool propWritten = false;
foreach (MemberInfo m in members)
{
FieldInfo f = m as FieldInfo;
PropertyInfo p = m as PropertyInfo;
if (f != null || p != null)
{
if (propWritten)
{
WriteTab();
}
else
{
propWritten = true;
}
Write(m.Name);
Write("=");
Type t = f != null ? f.FieldType : p.PropertyType;
if (t.IsValueType || t == typeof(string))
{
WriteValue(f != null ? f.GetValue(o) : p.GetValue(o, null));
}
else
{
if (typeof(IEnumerable).IsAssignableFrom(t))
{
Write("...");
}
else
{
Write("{ }");
}
}
}
}
if (propWritten) WriteLine();
if (level < depth)
{
foreach (MemberInfo m in members)
{
FieldInfo f = m as FieldInfo;
PropertyInfo p = m as PropertyInfo;
if (f != null || p != null)
{
Type t = f != null ? f.FieldType : p.PropertyType;
if (!(t.IsValueType || t == typeof(string)))
{
object value = f != null ? f.GetValue(o) : p.GetValue(o, null);
if (value != null)
{
level++;
WriteObject(m.Name + ": ", value);
level--;
}
}
}
}
}
}
}
private void WriteValue(object o)
{
if (o == null)
{
Write("null");
}
else if (o is DateTime)
{
Write(((DateTime)o).ToShortDateString());
}
else if (o is ValueType || o is string)
{
Write(o.ToString());
}
else if (o is IEnumerable)
{
Write("...");
}
else
{
Write("{ }");
}
}
}