This repository was archived by the owner on Nov 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenericToDataString.cs
More file actions
194 lines (173 loc) · 6.86 KB
/
GenericToDataString.cs
File metadata and controls
194 lines (173 loc) · 6.86 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace GenericToDataString
{
//http://stackoverflow.com/questions/852181/c-printing-all-properties-of-an-object
public class ObjectDumper
{
private int _currentIndent;
private readonly int _indentSize;
private readonly StringBuilder _stringBuilder;
private readonly Dictionary<object,int> _hashListOfFoundElements;
private readonly char _indentChar;
private readonly int _depth;
private int _currentLine;
private ObjectDumper(int depth, int indentSize, char indentChar)
{
_depth = depth;
_indentSize = indentSize;
_indentChar = indentChar;
_stringBuilder = new StringBuilder();
_hashListOfFoundElements = new Dictionary<object,int>();
}
public static string Dump(object element, int depth = 4,int indentSize=2,char indentChar=' ')
{
var instance = new ObjectDumper(depth, indentSize, indentChar);
return instance.DumpElement(element, true);
}
private string DumpElement(object element, bool isTopOfTree = false)
{
if (_currentIndent > _depth) { return null; }
if (element == null || element is string)
{
Write(FormatValue(element));
}
else if (element is ValueType)
{
Type objectType = element.GetType();
bool isWritten = false;
if (objectType.IsGenericType)
{
Type baseType = objectType.GetGenericTypeDefinition();
if (baseType == typeof(KeyValuePair<,>))
{
isWritten = true;
Write("Key:");
_currentIndent++;
DumpElement(objectType.GetProperty("Key").GetValue(element, null));
_currentIndent--;
Write("Value:");
_currentIndent++;
DumpElement(objectType.GetProperty("Value").GetValue(element, null));
_currentIndent--;
}
}
if (!isWritten)
{
Write(FormatValue(element));
}
}
else
{
if (element is IEnumerable enumerableElement)
{
foreach (object item in enumerableElement)
{
if (item is IEnumerable && !(item is string))
{
_currentIndent++;
DumpElement(item);
_currentIndent--;
}
else
{
DumpElement(item);
}
}
}
else
{
Type objectType = element.GetType();
Write("{{{0}(HashCode:{1})}}", objectType.FullName, element.GetHashCode());
if (!AlreadyDumped(element))
{
_currentIndent++;
MemberInfo[] members = objectType.GetMembers(BindingFlags.Public | BindingFlags.Instance);
foreach (var memberInfo in members)
{
var fieldInfo = memberInfo as FieldInfo;
var propertyInfo = memberInfo as PropertyInfo;
if (fieldInfo == null && (propertyInfo == null || !propertyInfo.CanRead || propertyInfo.GetIndexParameters().Length > 0))
continue;
var type = fieldInfo != null ? fieldInfo.FieldType : propertyInfo.PropertyType;
object value;
try
{
value = fieldInfo != null
? fieldInfo.GetValue(element)
: propertyInfo.GetValue(element, null);
}
catch (Exception e)
{
Write("{0} failed with:{1}", memberInfo.Name, (e.GetBaseException() ?? e).Message);
continue;
}
if (type.IsValueType || type == typeof(string))
{
Write("{0}: {1}", memberInfo.Name, FormatValue(value));
}
else
{
var isEnumerable = typeof(IEnumerable).IsAssignableFrom(type);
Write("{0}: {1}", memberInfo.Name, isEnumerable ? "..." : "{ }");
_currentIndent++;
DumpElement(value);
_currentIndent--;
}
}
_currentIndent--;
}
}
}
return isTopOfTree? _stringBuilder.ToString():null;
}
private bool AlreadyDumped(object value)
{
if (value == null)
return false;
if (_hashListOfFoundElements.TryGetValue(value, out int lineNo))
{
Write("(reference already dumped - line:{0})", lineNo);
return true;
}
_hashListOfFoundElements.Add(value, _currentLine);
return false;
}
private void Write(string value, params object[] args)
{
var space = new string(_indentChar, _currentIndent * _indentSize);
if (args != null)
value = string.Format(value, args);
_stringBuilder.AppendLine(space + value);
_currentLine++;
}
private string FormatValue(object o)
{
if (o == null)
return ("null");
if (o is DateTime)
return (((DateTime)o).ToShortDateString());
if (o is string)
return "\"" + (string)o + "\"";
if (o is char)
{
if (o.Equals('\0'))
{
return "''";
}
else
{
return "'" + (char)o + "'";
}
}
if (o is ValueType)
return (o.ToString());
if (o is IEnumerable)
return ("...");
return ("{ }");
}
}
}