This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectDumper.cs
339 lines (278 loc) · 10.9 KB
/
ObjectDumper.cs
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
namespace Bizi.Framework
{
public class ObjectDumper
{
private const string css = " <style type=\"text/css\" >"
+
@".domenabled #finderparent{border:1px solid #000;position:relative}.domenabled #finder{position:absolute;top:1em;left:1em}.domenabled ul#finder,.domenabled ul#finder li,.domenabled ul#finder ul{width:200px;list-style-type:none;margin:0;padding:0px}.domenabled ul#finder li{overflow:hidden;text-wrap:avoid;#fff-space:nowrap}.domenabled ul#finder ul.hidden{top:0px;left:-2000px;position:absolute}.domenabled ul#finder ul.shown{top:0px;left:240px;position:absolute}.domenabled #finder a.open{background:url(http://www.alistapart.com/d/complexdynamiclists/arrowon.gif) no-repeat 90% 50% #eee;padding-right:16px;padding-left:0px;display:block}.domenabled #finder a.parent{background:url(http://www.alistapart.com/d/complexdynamiclists/arrow.gif) no-repeat #fff 100% 50%;padding-right:16px;padding-left:0px}.domenabled ul#finder li a{color:#000;background:url(http://www.alistapart.com/d/complexdynamiclists/normal.gif) no-repeat #fff 0 50%;padding-left:16px;text-decoration:none}
</style>";
// TODO: Check if jquery is allready loaded before injection.
private const string js =
"<script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js\"></script>\n"
+ "<script type=\"text/javascript\">\n"
+
@"$(function () { var e = $('#finder'); if (!e) return; $('body').addClass('domenabled'); var t = 'parent', n = 'shown', r = 'hidden', i = 'open'; e.find('ul').addClass(r).find('ul:only-child').before('[item]'); var s = function () { var e = $(this), s = e.parent().parent(); return s.find('ul').each(function () { $(this).removeClass(n).addClass(r) }), s.find('a').each(function () { $(this).removeClass(i).addClass(t) }), e.removeClass(t).addClass(i), e.next('ul').addClass(n), !1 }; e.find('li:has(ul)').each(function () { var e = $(this).contents().filter(function () { return this.nodeName.toUpperCase() != 'UL' }), n = document.createElement('a'); n.href = '#', n.className = t, e.wrapAll(n).parent().click(s) }) })
</script>
";
private int depth;
protected int level;
protected int pos;
private TextWriter writer;
public virtual void Dump(object element, int depth, TextWriter log)
{
this.depth = depth;
this.writer = log;
Write(css);
Write(string.Format("<h1 title=\"{0}\">{1}</h1>", element.GetType(), element.GetType().Name));
Write("<div id=\"finderparent\">");
WriteObject(null, element);
Write("</div>");
Write(js);
}
public void Dump(object element)
{
Dump(element, 0);
}
public void Dump(object element, int depth)
{
Dump(element, depth, Console.Out);
}
protected virtual bool WriteCustom(object o)
{
return false;
}
protected virtual void WriteIndent()
{
for (int i = 0; i < this.level; i++)
this.writer.Write(" ");
}
protected virtual void WriteLine()
{
this.writer.WriteLine();
this.pos = 0;
}
protected virtual void WriteString(string value)
{
Write(value);
}
protected virtual void WriteTab()
{
Write(" ");
while (this.pos % 8 != 0)
Write(" ");
}
protected virtual void WriteValue(object o)
{
if (o == null)
Write("null");
else if (WriteCustom(o))
return;
else if (o is String)
WriteString((string)o);
else if (o is DateTime)
Write(((DateTime)o).ToShortDateString());
else if (o is ValueType)
Write(o.ToString());
else if (o is IEnumerable)
Write("...");
else
Write("{ }");
}
protected void Write(string s)
{
if (s == null)
return;
this.writer.Write(s);
this.pos += s.Length;
}
private void WriteObject(string prefix, object element)
{
if (element is Type && element.GetType().ToString() == "System.RuntimeType")
{
WriteIndent();
Write(prefix);
WriteValue(element.ToString());
WriteLine();
return;
}
if (element == null || element is ValueType || element is string || element is Uri)
{
WriteIndent();
Write(prefix);
WriteValue(element);
WriteLine();
return;
}
IEnumerable enumerableElement = element as IEnumerable;
if (enumerableElement != null)
{
var countElements = enumerableElement.Cast<object>().Count();
if (countElements == 0)
{
WriteIndent();
Write(prefix);
Write("Count = 0");
WriteLine();
return;
}
Write("<ul>");
foreach (object item in enumerableElement)
{
Write("<li>");
if (item is IEnumerable && !(item is string))
{
WriteIndent();
Write(prefix);
Write("...");
WriteLine();
if (this.level < this.depth)
{
this.level++;
WriteObject(prefix, item);
this.level--;
}
}
else
WriteObject(prefix, item);
Write("</li>");
}
Write("</ul>");
return;
}
Write(this.level == 0 ? "<ul id=\"finder\">" : "<ul>");
MemberInfo[] members = element.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;
bool fieldNotNull = f != null;
if (!fieldNotNull && p == null)
continue;
if (propWritten)
WriteTab();
else
propWritten = true;
Type t = fieldNotNull ? f.FieldType : p.PropertyType;
Write("<li>");
Write(string.Format("<strong title=\"{1}\">{0}</strong>", m.Name, t));
Write(": ");
object value = f != null ? f.GetValue(element) : p.GetValue(element, null);
if (t.IsValueType || t == typeof(string))
WriteValue(value);
else
{
if (this.level < this.depth)
{
this.level++;
WriteObject(null, value);
this.level--;
}
}
Write("</li>");
}
if (propWritten)
WriteLine();
/*if (this.level >= this.depth)
return;
foreach (MemberInfo m in members)
{
FieldInfo f = m as FieldInfo;
PropertyInfo p = m as PropertyInfo;
if (f == null && p == null)
continue;
Type t = f != null ? f.FieldType : p.PropertyType;
if ((t.IsValueType || t == typeof(string)))
continue;
object value = f != null ? f.GetValue(element) : p.GetValue(element, null);
if (value == null)
continue;
this.level++;
WriteObject("<h3>"+ m.Name + "</h3><ul>: ", value);
Write("</li>");
this.level--;
}*/
Write("</ul>");
}
}
public class HtmlObjectDumper : ObjectDumper
{
protected override bool WriteCustom(object o)
{
if (o is bool)
{
Write(string.Format("<input type=\"checkbox\" disabled=\"disabled\"{0}>",
((bool)o) == true ? " checked=\"checked\"" : ""));
return true;
}
Uri u = o as Uri;
if (u != null)
{
Write(string.Format("<a href=\"{0}\">{0}</a>", u.OriginalString));
return true;
}
return false;
}
protected override void WriteIndent()
{
/*for (int i = 0; i < this.level; i++)
Write("<span>...</span>");*/
}
protected override void WriteLine()
{
//Write("</br>");
this.pos = 0;
}
protected override void WriteString(string value)
{
Write(HttpUtility.HtmlEncode(value));
}
protected override void WriteTab()
{
//Write("<span>,,,</span>");
}
}
public static class ObjectExtensions
{
public static void Dump(this Object value)
{
new ObjectDumper().Dump(value);
}
public static void Dump(this Object value, int depth)
{
new ObjectDumper().Dump(value, depth);
}
public static void Dump(this Object value, int depth, TextWriter writer)
{
new ObjectDumper().Dump(value, depth, writer);
}
public static string DumpAsHtml(this Object value)
{
using (StringWriter stringWriter = new StringWriter())
{
new HtmlObjectDumper().Dump(value, 0, stringWriter);
return stringWriter.ToString();
}
}
public static string DumpAsHtml<T>(this T value, int depth)
{
using (StringWriter stringWriter = new StringWriter())
{
new HtmlObjectDumper().Dump(value, depth, stringWriter);
return stringWriter.ToString();
}
}
public static void DumpAsHtml(this Object value, int depth, TextWriter writer)
{
new HtmlObjectDumper().Dump(value, depth, writer);
}
}
}