|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Reflection; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace FINT.Model.Resource.Test |
| 8 | +{ |
| 9 | + public class ObjectDumper |
| 10 | + { |
| 11 | + private int _level; |
| 12 | + private readonly int _indentSize; |
| 13 | + private readonly StringBuilder _stringBuilder; |
| 14 | + private readonly List<int> _hashListOfFoundElements; |
| 15 | + |
| 16 | + private ObjectDumper(int indentSize) |
| 17 | + { |
| 18 | + _indentSize = indentSize; |
| 19 | + _stringBuilder = new StringBuilder(); |
| 20 | + _hashListOfFoundElements = new List<int>(); |
| 21 | + } |
| 22 | + |
| 23 | + public static string Dump(object element) |
| 24 | + { |
| 25 | + return Dump(element, 2); |
| 26 | + } |
| 27 | + |
| 28 | + public static string Dump(object element, int indentSize) |
| 29 | + { |
| 30 | + var instance = new ObjectDumper(indentSize); |
| 31 | + return instance.DumpElement(element); |
| 32 | + } |
| 33 | + |
| 34 | + private string DumpElement(object element) |
| 35 | + { |
| 36 | + if (element == null || element is ValueType || element is string) |
| 37 | + { |
| 38 | + Write(FormatValue(element)); |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + var objectType = element.GetType(); |
| 43 | + if (!typeof(IEnumerable).IsAssignableFrom(objectType)) |
| 44 | + { |
| 45 | + Write("{{{0}}}", objectType.FullName); |
| 46 | + _hashListOfFoundElements.Add(element.GetHashCode()); |
| 47 | + _level++; |
| 48 | + } |
| 49 | + |
| 50 | + var enumerableElement = element as IEnumerable; |
| 51 | + if (enumerableElement != null) |
| 52 | + { |
| 53 | + foreach (object item in enumerableElement) |
| 54 | + { |
| 55 | + if (item is IEnumerable && !(item is string)) |
| 56 | + { |
| 57 | + _level++; |
| 58 | + DumpElement(item); |
| 59 | + _level--; |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + if (!AlreadyTouched(item)) |
| 64 | + DumpElement(item); |
| 65 | + else |
| 66 | + Write("{{{0}}} <-- bidirectional reference found", item.GetType().FullName); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance); |
| 73 | + foreach (var memberInfo in members) |
| 74 | + { |
| 75 | + var fieldInfo = memberInfo as FieldInfo; |
| 76 | + var propertyInfo = memberInfo as PropertyInfo; |
| 77 | + |
| 78 | + if (fieldInfo == null && propertyInfo == null) |
| 79 | + continue; |
| 80 | + |
| 81 | + var type = fieldInfo != null ? fieldInfo.FieldType : propertyInfo.PropertyType; |
| 82 | + object value = fieldInfo != null |
| 83 | + ? fieldInfo.GetValue(element) |
| 84 | + : propertyInfo.GetValue(element, null); |
| 85 | + |
| 86 | + if (type.IsValueType || type == typeof(string)) |
| 87 | + { |
| 88 | + Write("{0}: {1}", memberInfo.Name, FormatValue(value)); |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + var isEnumerable = typeof(IEnumerable).IsAssignableFrom(type); |
| 93 | + Write("{0}: {1}", memberInfo.Name, isEnumerable ? "..." : "{ }"); |
| 94 | + |
| 95 | + var alreadyTouched = !isEnumerable && AlreadyTouched(value); |
| 96 | + _level++; |
| 97 | + if (!alreadyTouched) |
| 98 | + DumpElement(value); |
| 99 | + else |
| 100 | + Write("{{{0}}} <-- bidirectional reference found", value.GetType().FullName); |
| 101 | + _level--; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (!typeof(IEnumerable).IsAssignableFrom(objectType)) |
| 107 | + { |
| 108 | + _level--; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return _stringBuilder.ToString(); |
| 113 | + } |
| 114 | + |
| 115 | + private bool AlreadyTouched(object value) |
| 116 | + { |
| 117 | + if (value == null) |
| 118 | + return false; |
| 119 | + |
| 120 | + var hash = value.GetHashCode(); |
| 121 | + for (var i = 0; i < _hashListOfFoundElements.Count; i++) |
| 122 | + { |
| 123 | + if (_hashListOfFoundElements[i] == hash) |
| 124 | + return true; |
| 125 | + } |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + private void Write(string value, params object[] args) |
| 130 | + { |
| 131 | + var space = new string(' ', _level * _indentSize); |
| 132 | + |
| 133 | + if (args != null) |
| 134 | + value = string.Format(value, args); |
| 135 | + |
| 136 | + _stringBuilder.AppendLine(space + value); |
| 137 | + } |
| 138 | + |
| 139 | + private string FormatValue(object o) |
| 140 | + { |
| 141 | + if (o == null) |
| 142 | + return ("null"); |
| 143 | + |
| 144 | + if (o is DateTime) |
| 145 | + return (((DateTime)o).ToShortDateString()); |
| 146 | + |
| 147 | + if (o is string) |
| 148 | + return string.Format("\"{0}\"", o); |
| 149 | + |
| 150 | + if (o is char && (char)o == '\0') |
| 151 | + return string.Empty; |
| 152 | + |
| 153 | + if (o is ValueType) |
| 154 | + return (o.ToString()); |
| 155 | + |
| 156 | + if (o is IEnumerable) |
| 157 | + return ("..."); |
| 158 | + |
| 159 | + return ("{ }"); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments