ObjectDumper re-loaded

Sometimes in history a star flashes for a short moment to shade for the next. The same happened to the class ObjectDumper which is quite a handy class for writing NUnit Test cases – published one time in a microsoft sample – and after that never found again.

The performance of that class used with NUnit was terrible, because the ObjectDumper wrote directly to the Console Out Stream. That forced NUnit to refresh the UI again and again. I improved the ObjectDumper, so that it takes a StringBuilder and write the complete output to the Console in one step. That’s faster.

You can output the content of every object using the following syntax:

ObjectDumper.Write (object you want to output);

Cheers
- Gerhard

kick it on DotNetKicks.com



public class ObjectDumper {
    
    public static void Write(object o) {
        Write(o, 0);
    }
    
    public static void Write(object o, int depth) {
        ObjectDumper dumper = new ObjectDumper(depth);
        dumper.WriteObject(null, o);
        Console.Write(dumper.ToString());
    }
    
    private StringBuilder builder = new StringBuilder();
    
    int pos;
    int level;
    int depth;
    
    public override string ToString()
    {
        return builder.ToString();
    }
    
    private ObjectDumper(int depth) {
        this.depth = depth;
    }
    
    private void Write(string s) {
        if (s != null) {
            builder.Append(s);
            pos += s.Length;
        }
    }
    
    private void WriteIndent() {
        builder.Append(' ', level * 3);
    }
    
    private void WriteLine() {
        builder.AppendLine();
        pos = 0;
    }
    
    private void WriteTab() {
        Write("\t");
    }
    
    private void WriteObject(string prefix, object o) {
        
        if (o == null || o is ValueType || o is string) {
            WriteIndent();
            Write(prefix);
            WriteValue(o);
            WriteLine();
        }
        else if (o is IEnumerable) {
            foreach (object element in (IEnumerable)o) {
                if (element is IEnumerable && !(element is string)) {
                    WriteIndent();
                    Write(prefix);
                    Write("...");
                    WriteLine();
                    if (level < depth) {
                        level++;
                        WriteObject(prefix, element);
                        level--;
                    }
                }
                else {
                    WriteObject(prefix, element);
                }
            }
        }
        else {
            MemberInfo[] members = o.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance);
            WriteIndent();
            Write(prefix);
            bool propWritten = false;
            foreach (MemberInfo m in members) {
                FieldInfo f = m as FieldInfo;
                PropertyInfo p = m as PropertyInfo;
                if (f != null || p != null) {
                    if (propWritten) {
                        WriteTab();
                    }
                    else {
                        propWritten = true;
                    }
                    Write(m.Name);
                    Write("=");
                    Type t = f != null ? f.FieldType : p.PropertyType;
                    if (t.IsValueType || t == typeof(string)) {
                        WriteValue(f != null ? f.GetValue(o) : p.GetValue(o, null));
                    }
                    else {
                        if (typeof(IEnumerable).IsAssignableFrom(t)) {
                            Write("...");
                        }
                        else {
                            Write("{ }");
                        }
                    }
                }
            }
            if (propWritten) WriteLine();
            if (level < depth) {
                foreach (MemberInfo m in members) {
                    FieldInfo f = m as FieldInfo;
                    PropertyInfo p = m as PropertyInfo;
                    if (f != null || p != null) {
                        Type t = f != null ? f.FieldType : p.PropertyType;
                        if (!(t.IsValueType || t == typeof(string))) {
                            object value = f != null ? f.GetValue(o) : p.GetValue(o, null);
                            if (value != null) {
                                level++;
                                WriteObject(m.Name + ": ", value);
                                level--;
                            }
                        }
                    }
                }
            }
        }
    }
    
    private void WriteValue(object o) {
        if (o == null) {
            Write("null");
        }
        else if (o is DateTime) {
            Write(((DateTime)o).ToShortDateString());
        }
        else if (o is ValueType || o is string) {
            Write(o.ToString());
        }
        else if (o is IEnumerable) {
            Write("...");
        }
        else {
            Write("{ }");
        }
    }
}

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 106 other followers

%d bloggers like this: