Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ObjectDumper
- {
- private ObjectDumper(int indentSize)
- {
- this._indentSize = indentSize;
- this._stringBuilder = new StringBuilder();
- this._hashListOfFoundElements = new List<int>();
- }
- public static string dump(object element)
- {
- return Methods.ObjectDumper.dump(element, 2);
- }
- public static string dump(object element, int indentSize)
- {
- Methods.ObjectDumper objectDumper = new Methods.ObjectDumper(indentSize);
- return objectDumper.DumpElement(element);
- }
- private string DumpElement(object element)
- {
- if (element == null || element is ValueType || element is string)
- {
- this.Write(this.FormatValue(element), new object[0]);
- }
- else
- {
- Type type = element.GetType();
- if (!typeof(IEnumerable).IsAssignableFrom(type))
- {
- this.Write("{{{0}}}", new object[]
- {
- type.FullName
- });
- this._hashListOfFoundElements.Add(element.GetHashCode());
- this._level++;
- }
- IEnumerable enumerable = element as IEnumerable;
- if (enumerable != null)
- {
- using (IEnumerator enumerator = enumerable.GetEnumerator())
- {
- while (enumerator.MoveNext())
- {
- object obj = enumerator.Current;
- if (obj is IEnumerable && !(obj is string))
- {
- this._level++;
- this.DumpElement(obj);
- this._level--;
- }
- else if (!this.AlreadyTouched(obj))
- {
- this.DumpElement(obj);
- }
- else
- {
- this.Write("{{{0}}} <-- bidirectional reference found", new object[]
- {
- obj.GetType().FullName
- });
- }
- }
- goto IL_2BB;
- }
- }
- MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public);
- foreach (MemberInfo memberInfo in members)
- {
- FieldInfo fieldInfo = memberInfo as FieldInfo;
- PropertyInfo propertyInfo = memberInfo as PropertyInfo;
- if (!(fieldInfo == null) || !(propertyInfo == null))
- {
- Type type2 = (fieldInfo != null) ? fieldInfo.FieldType : propertyInfo.PropertyType;
- object obj2 = (fieldInfo != null) ? fieldInfo.GetValue(element) : propertyInfo.GetValue(element, null);
- if (type2.IsValueType || type2 == typeof(string))
- {
- this.Write("{0}: {1}", new object[]
- {
- memberInfo.Name,
- this.FormatValue(obj2)
- });
- }
- else
- {
- bool flag = typeof(IEnumerable).IsAssignableFrom(type2);
- this.Write("{0}: {1}", new object[]
- {
- memberInfo.Name,
- flag ? "..." : "{ }"
- });
- bool flag2 = !flag && this.AlreadyTouched(obj2);
- this._level++;
- if (!flag2)
- {
- this.DumpElement(obj2);
- }
- else
- {
- this.Write("{{{0}}} <-- bidirectional reference found", new object[]
- {
- obj2.GetType().FullName
- });
- }
- this._level--;
- }
- }
- }
- IL_2BB:
- if (!typeof(IEnumerable).IsAssignableFrom(type))
- {
- this._level--;
- }
- }
- return this._stringBuilder.ToString();
- }
- private bool AlreadyTouched(object value)
- {
- if (value == null)
- {
- return false;
- }
- int hashCode = value.GetHashCode();
- for (int i = 0; i < this._hashListOfFoundElements.Count; i++)
- {
- if (this._hashListOfFoundElements[i] == hashCode)
- {
- return true;
- }
- }
- return false;
- }
- private void Write(string value, params object[] args)
- {
- string str = new string(' ', this._level * this._indentSize);
- if (args != null)
- {
- value = string.Format(value, args);
- }
- this._stringBuilder.AppendLine(str + value);
- }
- private string FormatValue(object o)
- {
- if (o == null)
- {
- return "null";
- }
- if (o is DateTime)
- {
- return ((DateTime)o).ToShortDateString();
- }
- if (o is string)
- {
- return string.Format("\"{0}\"", o);
- }
- if (o is char && (char)o == '\0')
- {
- return string.Empty;
- }
- if (o is ValueType)
- {
- return o.ToString();
- }
- if (o is IEnumerable)
- {
- return "...";
- }
- return "{ }";
- }
- private int _level;
- private readonly int _indentSize;
- private readonly StringBuilder _stringBuilder;
- private readonly List<int> _hashListOfFoundElements;
- }
Add Comment
Please, Sign In to add comment