|
| 1 | +// Copyright © Microsoft Corporation. All Rights Reserved. |
| 2 | +// This code released under the terms of the |
| 3 | +// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) |
| 4 | +// |
| 5 | +//Copyright (C) Microsoft Corporation. All rights reserved. |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections; |
| 9 | +using System.IO; |
| 10 | +using System.Reflection; |
| 11 | + |
| 12 | +namespace FluentSpecification.Samples |
| 13 | +{ |
| 14 | + public class ObjectDumper { |
| 15 | + |
| 16 | + public static void Write(object element) |
| 17 | + { |
| 18 | + Write(element, 0); |
| 19 | + } |
| 20 | + |
| 21 | + public static void Write(object element, int depth) |
| 22 | + { |
| 23 | + Write(element, depth, Console.Out); |
| 24 | + } |
| 25 | + |
| 26 | + public static void Write(object element, int depth, TextWriter log) |
| 27 | + { |
| 28 | + ObjectDumper dumper = new ObjectDumper(depth); |
| 29 | + dumper.writer = log; |
| 30 | + dumper.WriteObject(null, element); |
| 31 | + } |
| 32 | + |
| 33 | + TextWriter writer; |
| 34 | + int pos; |
| 35 | + int level; |
| 36 | + int depth; |
| 37 | + |
| 38 | + private ObjectDumper(int depth) |
| 39 | + { |
| 40 | + this.depth = depth; |
| 41 | + } |
| 42 | + |
| 43 | + private void Write(string s) |
| 44 | + { |
| 45 | + if (s != null) { |
| 46 | + writer.Write(s); |
| 47 | + pos += s.Length; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private void WriteIndent() |
| 52 | + { |
| 53 | + for (int i = 0; i < level; i++) writer.Write(" "); |
| 54 | + } |
| 55 | + |
| 56 | + private void WriteLine() |
| 57 | + { |
| 58 | + writer.WriteLine(); |
| 59 | + pos = 0; |
| 60 | + } |
| 61 | + |
| 62 | + private void WriteTab() |
| 63 | + { |
| 64 | + Write(" "); |
| 65 | + while (pos % 8 != 0) Write(" "); |
| 66 | + } |
| 67 | + |
| 68 | + private void WriteObject(string prefix, object element) |
| 69 | + { |
| 70 | + |
| 71 | + |
| 72 | + if (element == null || element is ValueType || element is string) { |
| 73 | + WriteIndent(); |
| 74 | + Write(prefix); |
| 75 | + WriteValue(element); |
| 76 | + WriteLine(); |
| 77 | + } |
| 78 | + else { |
| 79 | + IEnumerable enumerableElement = element as IEnumerable; |
| 80 | + if (enumerableElement != null) { |
| 81 | + foreach (object item in enumerableElement) { |
| 82 | + if (item is IEnumerable && !(item is string)) { |
| 83 | + WriteIndent(); |
| 84 | + Write(prefix); |
| 85 | + Write("..."); |
| 86 | + WriteLine(); |
| 87 | + if (level < depth) { |
| 88 | + level++; |
| 89 | + WriteObject(prefix, item); |
| 90 | + level--; |
| 91 | + } |
| 92 | + } |
| 93 | + else { |
| 94 | + WriteObject(prefix, item); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + else { |
| 99 | + MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance); |
| 100 | + WriteIndent(); |
| 101 | + Write(prefix); |
| 102 | + bool propWritten = false; |
| 103 | + foreach (MemberInfo m in members) { |
| 104 | + FieldInfo f = m as FieldInfo; |
| 105 | + PropertyInfo p = m as PropertyInfo; |
| 106 | + if (f != null || p != null) { |
| 107 | + if (propWritten) { |
| 108 | + WriteTab(); |
| 109 | + } |
| 110 | + else { |
| 111 | + propWritten = true; |
| 112 | + } |
| 113 | + Write(m.Name); |
| 114 | + Write("="); |
| 115 | + Type t = f != null ? f.FieldType : p.PropertyType; |
| 116 | + if (t.IsValueType || t == typeof(string)) { |
| 117 | + WriteValue(f != null ? f.GetValue(element) : p.GetValue(element, null)); |
| 118 | + } |
| 119 | + else { |
| 120 | + if (typeof(IEnumerable).IsAssignableFrom(t)) { |
| 121 | + Write("..."); |
| 122 | + } |
| 123 | + else { |
| 124 | + Write("{ }"); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + if (propWritten) WriteLine(); |
| 130 | + if (level < depth) { |
| 131 | + foreach (MemberInfo m in members) { |
| 132 | + FieldInfo f = m as FieldInfo; |
| 133 | + PropertyInfo p = m as PropertyInfo; |
| 134 | + if (f != null || p != null) { |
| 135 | + Type t = f != null ? f.FieldType : p.PropertyType; |
| 136 | + if (!(t.IsValueType || t == typeof(string))) { |
| 137 | + object value = f != null ? f.GetValue(element) : p.GetValue(element, null); |
| 138 | + if (value != null) { |
| 139 | + level++; |
| 140 | + WriteObject(m.Name + ": ", value); |
| 141 | + level--; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private void WriteValue(object o) |
| 152 | + { |
| 153 | + if (o == null) { |
| 154 | + Write("null"); |
| 155 | + } |
| 156 | + else if (o is DateTime) { |
| 157 | + Write(((DateTime)o).ToShortDateString()); |
| 158 | + } |
| 159 | + else if (o is ValueType || o is string) { |
| 160 | + Write(o.ToString()); |
| 161 | + } |
| 162 | + else if (o is IEnumerable) { |
| 163 | + Write("..."); |
| 164 | + } |
| 165 | + else { |
| 166 | + Write("{ }"); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments