-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dumper.cs
176 lines (163 loc) · 6.87 KB
/
Dumper.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace DTZ.Utilities
{
public static class DumperExtensions
{
public static string DumpToString(this Object a)
{
return new Dumper(a).ToString();
}
public static IEnumerable<T> DumpToConsole<T>(this IEnumerable<T> enumerable)
{
foreach (T a in enumerable)
{
Console.Out.WriteLine(DumpToString(enumerable));
yield return a;
}
}
public static T DumpToConsole<T>(this T a)
{
Console.Out.WriteLine(DumpToString(a));
return a;
}
}
public class Dumper
{
private readonly object _a;
private readonly IList<object> _l = new List<object>();
private readonly IndentedStringBuilder _sb = new IndentedStringBuilder();
public Dumper(Object a)
{
_a = a;
}
public static string ToGenericTypeString(Type t)
{
if (t.Name.Contains("__AnonymousType")) return t.IsArray ? "[]" : "";
if (!t.IsGenericType)
return t.Name;
string genericTypeName = t.GetGenericTypeDefinition().Name;
int indexOf = genericTypeName.IndexOf('`');
if (indexOf > 0)
genericTypeName = genericTypeName.Substring(0, indexOf);
string genericArgs = string.Join(",", t.GetGenericArguments().Select(ToGenericTypeString).ToArray());
return genericTypeName + "<" + genericArgs + ">";
}
private void Dump(Object o)
{
if (o == null) _sb.Append("null");
else
{
Type type = o.GetType();
Type genType = type.IsGenericType ? type.GetGenericTypeDefinition() : null;
if (type.IsEnum) _sb.AppendFormat("{0}.{1}", type.Name, Enum.GetName(type, o));
else if (type == typeof (string))
{
string s = o.ToString();
if (!s.Any(b => b == '\r' || b == '\n' || b == '"' || b == '\\'))
_sb.AppendFormat("\"{0}\"", s);
else
_sb.AppendFormat(@"@""{0}""", s.Replace(@"""", @""""""));
}
else if (type == typeof (bool)) _sb.Append((bool) o ? "true" : "false");
else if (type == typeof (Guid)) _sb.AppendFormat(@"new Guid(""{0}"")", o);
else if (type == typeof (DateTime)) _sb.AppendFormat(@"DateTime.ParseExact(""{0:r}"", ""r"")", o);
else if (genType == typeof (KeyValuePair<,>))
{
_sb.Append("{");
Dump(type.GetProperty("Key").GetValue(o, null));
_sb.Append(", ");
Dump(type.GetProperty("Value").GetValue(o, null));
_sb.Append("}");
}
else if (type.IsValueType) _sb.Append(o.ToString());
else if (type.IsSubclassOf(typeof (Type))) _sb.Append(o.ToString());
else
{
if (_l.Contains(o)) // This breaks circular references
{
_sb.Append("#Ref");
return;
}
_l.Add(o);
string genericTypeString = ToGenericTypeString(type);
_sb.AppendFormat(string.IsNullOrEmpty(genericTypeString) ? "new {0}{{" : "new {0} {{", genericTypeString);
using (_sb.IncreaseIndent())
{
var dictionary = o as IDictionary;
if (dictionary != null)
{
int i = 0;
foreach (DictionaryEntry b in dictionary)
using (_sb.IncreaseIndent())
{
if (i == 0) _sb.AppendLine();
if (i++ > 0) _sb.Append(",\r\n");
_sb.Append("{");
Dump(b.Key);
_sb.Append(", ");
Dump(b.Value);
_sb.Append("}");
}
if (i != 0) _sb.AppendLine();
}
else
{
var enumerable = o as IEnumerable;
if (enumerable != null)
{
int i = 0;
foreach (object b in enumerable)
{
if (i == 0) _sb.AppendLine();
if (i++ > 0) _sb.Append(",\r\n");
Dump(b);
}
if (i != 0) _sb.AppendLine();
}
else
{
int i = 0;
foreach (PropertyInfo info in type.GetProperties())
{
if (info.GetIndexParameters().Length != 0) continue;
if (info.Name != "SyncRoot" && info.Name != "ExtensionData")
{
if (i == 0) _sb.AppendLine();
_sb.AppendFormat(i++ == 0 ? "{0} = " : ",\r\n{0} = ", info.Name);
object value = GetValue(o, info);
if (value == o)
_sb.Append("this");
else
Dump(value);
}
}
if (i != 0) _sb.AppendLine();
}
}
}
_sb.Append("}");
}
}
}
private static object GetValue(object a, PropertyInfo info)
{
try
{
return info.GetValue(a, null);
}
catch (Exception)
{
return "#Err";
}
}
public override string ToString()
{
if (_sb.Length == 0) Dump(_a);
return _sb.ToString();
}
}
}