How to dump object properties

I needed a quick’n’dirty way to inspect objects returned from external source. I wrote simple object properties dumping mechanism you can use to investigate unknown objects. It is really quick and really dirty.


using System.Collections.Generic;
using System.IO;
 
namespace UlmeTeenused
{
    public class ObjectDump
    {
        public static void Write(TextWriter writer, object obj)
        {
            if (obj == null)
            {
                writer.WriteLine("Object is null");
                return;
            }
 
            writer.Write("Hash: ");
            writer.WriteLine(obj.GetHashCode());
            writer.Write("Type: ");
            writer.WriteLine(obj.GetType());
 
            var props = GetProperties(obj);
 
            if (props.Count > 0)
            {
                writer.WriteLine("-------------------------");
            }
 
            foreach (var prop in props)
            {
                writer.Write(prop.Key);
                writer.Write(": ");
                writer.WriteLine(prop.Value);
            }
        }
 
        private static Dictionary<string, string> GetProperties(object obj)
        {
            var props = new Dictionary<string, string>();
            if (obj == null)
                return props;
 
            var type = obj.GetType();
            foreach (var prop in type.GetProperties())
            {
                var val = prop.GetValue(obj, new object[] { });
                var valStr = val == null ? "" : val.ToString();
                props.Add(prop.Name, valStr);
            }
 
            return props;
        }
    }
}

If you are using console application you can create properties dump using the following code.


ObjectDump.Write(Console.Out,objFromSrv);

The result is something like this.

Object properties dump

And we are done. This code is not intended to use in live environments. Use it when you are investigating different mysteries created by someone else. :)

5 Comments

  • objectdumper.cs has been available for a very long time from Microsoft to dump properties of an object to console. You can use that as well and tweak it to suit your needs.

  • Is there a reason you didn't use WMI instrumentation to achieve this?

  • Made some additions so it can handle ICollections as well:

    public class ObjectDump
    {
    private static int depth = -1;
    private static int MAX_DEPTH = 10;

    public static void Write(TextWriter writer, object obj, int maxDepth)
    {
    int oldMaxDepth = MAX_DEPTH;
    MAX_DEPTH = maxDepth;
    Write(writer, obj);
    MAX_DEPTH = oldMaxDepth;
    }

    public static void Write(TextWriter writer, object obj)
    {
    if (obj == null)
    {
    writer.WriteLine("Object is null");
    return;
    }

    // Prevent from endless recursion
    if (depth > MAX_DEPTH)
    {
    //writer.WriteLine("Max depth reached ({0})", MAX_DEPTH);
    return;
    }

    depth++;

    String depthStr = "";
    for (int i = 0; i 0)
    depthStr += "| ";

    //writer.WriteLine("{0}Hash: {1}", depthStr, obj.GetHashCode());
    writer.Write("{0}Type: {1}", depthStr, obj.GetType());

    if (obj is ICollection)
    {
    writer.WriteLine(" Count ( {0} )", ((ICollection)obj).Count);
    writer.WriteLine("{0}-----------------------------------------------------------------------", depthStr);
    foreach (object element in ((ICollection)obj))
    {
    Write(writer, element);
    }
    }
    else {
    writer.WriteLine();
    writer.WriteLine("{0}---------------------[ Properties ] -----------------------------------", depthStr);
    var props = obj.GetType().GetProperties();
    foreach (var prop in props)
    {
    // Get the property value
    object val = prop.GetValue(obj, new object[] { });
    var valStr = val == null ? "" : val.ToString();
    writer.Write("{0}- {1}: {2}", depthStr,prop.Name, valStr);
    if (val is ICollection)
    {
    writer.WriteLine(" Count ( {0} )", ((ICollection)val).Count);
    writer.WriteLine("{0}-----------------------------------------------------------------------", depthStr);
    foreach (object element in ((ICollection)val))
    {
    Write(writer, element);
    writer.WriteLine("..{0}-----------------------------------------------------------------------", depthStr);
    }

    }
    else { writer.WriteLine(); }
    }
    }
    //writer.WriteLine();
    depth--;
    }
    }

  • maybe convert this to an extension method

    public static class ObjectDump
    {
    public static void Write(this T obj, TextWriter writer)
    {
    ....
    }
    }

  • How to dump object properties.. Nifty :)

Comments have been disabled for this content.