-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDumper.cs
More file actions
86 lines (81 loc) · 2.95 KB
/
Dumper.cs
File metadata and controls
86 lines (81 loc) · 2.95 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Dumper
{
public static class ObjectDumper
{
private static readonly HashSet<object> DumpedObjects = new HashSet<object>();
public static string Dump(this object obj)
{
DumpedObjects.Clear();
if (obj == null) return "null object";
var type = obj.GetType();
return DumpObject(obj, 0, type.Name);
}
private static string ParseArray(IList list,int depth)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
var type = list[i].GetType();
sb.AppendLine(list[i].DumpObject(depth, type.Name + ",Index " + (i).ToString()));
}
return sb.ToString();
}
private static string DumpObject(this object obj, int depth, string name = null)
{
if (obj == null)
return GetLine(name, "null", depth);
var type = obj.GetType();
if (type.IsPrimitive || type == typeof (string) || type.IsEnum)
return GetLine(name, obj.ToString(), depth);
if (!type.IsValueType)
{
if (DumpedObjects.Contains(obj))
return GetLine(name, "Already dumped", depth);
DumpedObjects.Add(obj);
}
StringBuilder builder = new StringBuilder();
if (type.IsArray || typeof (IList).IsAssignableFrom(type))
{
builder.AppendLine(GetLine(name, null, depth));
builder.AppendLine(ParseArray(obj as IList, depth + 1));
}
else if (type.IsClass)
{
builder.AppendLine(GetLine(name, null, depth));
var propertys = type.GetProperties();
if (!propertys.Any())
builder.AppendLine(GetLine(name, obj.ToString(), depth));
foreach (var p in propertys)
{
try
{
var value = p.GetValue(obj, null);
var line = DumpObject(value, depth + 1, p.Name);
builder.AppendLine(line);
}
catch (Exception ex)
{
string msg = GetLine(name, ex.Message, depth + 1);
builder.AppendLine(msg);
}
}
}
else
{
builder.AppendLine(GetLine(name, obj.ToString(), depth));
}
return builder.ToString();
}
static string GetLine(string name, string value, int depth)
{
string indent = new string(' ', depth*2);
var line = string.Format("{0}{1}: {2}", indent, name, value);
return line;
}
}
}