retesere20

c#-dumper-2

Sep 9th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1.  
  2. public class ObjectDumper
  3. {
  4.  
  5. private ObjectDumper(int indentSize)
  6. {
  7. this._indentSize = indentSize;
  8. this._stringBuilder = new StringBuilder();
  9. this._hashListOfFoundElements = new List<int>();
  10. }
  11.  
  12.  
  13. public static string dump(object element)
  14. {
  15. return Methods.ObjectDumper.dump(element, 2);
  16. }
  17.  
  18.  
  19. public static string dump(object element, int indentSize)
  20. {
  21. Methods.ObjectDumper objectDumper = new Methods.ObjectDumper(indentSize);
  22. return objectDumper.DumpElement(element);
  23. }
  24.  
  25.  
  26. private string DumpElement(object element)
  27. {
  28. if (element == null || element is ValueType || element is string)
  29. {
  30. this.Write(this.FormatValue(element), new object[0]);
  31. }
  32. else
  33. {
  34. Type type = element.GetType();
  35. if (!typeof(IEnumerable).IsAssignableFrom(type))
  36. {
  37. this.Write("{{{0}}}", new object[]
  38. {
  39. type.FullName
  40. });
  41. this._hashListOfFoundElements.Add(element.GetHashCode());
  42. this._level++;
  43. }
  44. IEnumerable enumerable = element as IEnumerable;
  45. if (enumerable != null)
  46. {
  47. using (IEnumerator enumerator = enumerable.GetEnumerator())
  48. {
  49. while (enumerator.MoveNext())
  50. {
  51. object obj = enumerator.Current;
  52. if (obj is IEnumerable && !(obj is string))
  53. {
  54. this._level++;
  55. this.DumpElement(obj);
  56. this._level--;
  57. }
  58. else if (!this.AlreadyTouched(obj))
  59. {
  60. this.DumpElement(obj);
  61. }
  62. else
  63. {
  64. this.Write("{{{0}}} <-- bidirectional reference found", new object[]
  65. {
  66. obj.GetType().FullName
  67. });
  68. }
  69. }
  70. goto IL_2BB;
  71. }
  72. }
  73. MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public);
  74. foreach (MemberInfo memberInfo in members)
  75. {
  76. FieldInfo fieldInfo = memberInfo as FieldInfo;
  77. PropertyInfo propertyInfo = memberInfo as PropertyInfo;
  78. if (!(fieldInfo == null) || !(propertyInfo == null))
  79. {
  80. Type type2 = (fieldInfo != null) ? fieldInfo.FieldType : propertyInfo.PropertyType;
  81. object obj2 = (fieldInfo != null) ? fieldInfo.GetValue(element) : propertyInfo.GetValue(element, null);
  82. if (type2.IsValueType || type2 == typeof(string))
  83. {
  84. this.Write("{0}: {1}", new object[]
  85. {
  86. memberInfo.Name,
  87. this.FormatValue(obj2)
  88. });
  89. }
  90. else
  91. {
  92. bool flag = typeof(IEnumerable).IsAssignableFrom(type2);
  93. this.Write("{0}: {1}", new object[]
  94. {
  95. memberInfo.Name,
  96. flag ? "..." : "{ }"
  97. });
  98. bool flag2 = !flag && this.AlreadyTouched(obj2);
  99. this._level++;
  100. if (!flag2)
  101. {
  102. this.DumpElement(obj2);
  103. }
  104. else
  105. {
  106. this.Write("{{{0}}} <-- bidirectional reference found", new object[]
  107. {
  108. obj2.GetType().FullName
  109. });
  110. }
  111. this._level--;
  112. }
  113. }
  114. }
  115. IL_2BB:
  116. if (!typeof(IEnumerable).IsAssignableFrom(type))
  117. {
  118. this._level--;
  119. }
  120. }
  121. return this._stringBuilder.ToString();
  122. }
  123.  
  124.  
  125. private bool AlreadyTouched(object value)
  126. {
  127. if (value == null)
  128. {
  129. return false;
  130. }
  131. int hashCode = value.GetHashCode();
  132. for (int i = 0; i < this._hashListOfFoundElements.Count; i++)
  133. {
  134. if (this._hashListOfFoundElements[i] == hashCode)
  135. {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141.  
  142.  
  143. private void Write(string value, params object[] args)
  144. {
  145. string str = new string(' ', this._level * this._indentSize);
  146. if (args != null)
  147. {
  148. value = string.Format(value, args);
  149. }
  150. this._stringBuilder.AppendLine(str + value);
  151. }
  152.  
  153.  
  154. private string FormatValue(object o)
  155. {
  156. if (o == null)
  157. {
  158. return "null";
  159. }
  160. if (o is DateTime)
  161. {
  162. return ((DateTime)o).ToShortDateString();
  163. }
  164. if (o is string)
  165. {
  166. return string.Format("\"{0}\"", o);
  167. }
  168. if (o is char && (char)o == '\0')
  169. {
  170. return string.Empty;
  171. }
  172. if (o is ValueType)
  173. {
  174. return o.ToString();
  175. }
  176. if (o is IEnumerable)
  177. {
  178. return "...";
  179. }
  180. return "{ }";
  181. }
  182.  
  183.  
  184. private int _level;
  185.  
  186.  
  187. private readonly int _indentSize;
  188.  
  189.  
  190. private readonly StringBuilder _stringBuilder;
  191.  
  192.  
  193. private readonly List<int> _hashListOfFoundElements;
  194. }
Add Comment
Please, Sign In to add comment