All about ObjectDumper
All About ObjectDumper
Why do I need it for?
In many demos, we print objects to the Console window. If the object is a very simple one, like:
public class Vehicle
{
public string VehicleID { get; set; }
public int Year { get; set; }
public string Model { get; set; }
}
than it is very easy:
Vehicle v = new Vehicle { VehicleID = "30-804-15", Model = "Mazda 3", Year = 2006 };
Console.WriteLine("VehicleID = {0}, Model = {1}, Year = {2}", v.VehicleID, v.Model, v.Year);

But what if the object has many properties? What if we want to print only the properties that contain values that are not null? What if the object is not flat, but a collection of objects, or an object graph with depth of more than 1?
During demos, writing the appropriate code can be time-consuming.
What is ObjectDumper?
ObjectDumper can be very useful in those situations. It can print simple to complex objects and object graphs in a single line of code.
For exmaple, the above simple object:
Vehicle v = new Vehicle { VehicleID = "30-804-15", Model = "Mazda 3", Year = 2006 };
ObjectDumper.Write(v);
It can print a list of objects:
Vehicle[] v = new Vehicle[]
{
new Vehicle { VehicleID = "30-804-15", Model = "Mazda 3", Year = 2006 },
new Vehicle { VehicleID = "29-123-43", Model = "Subaru B4", Year = 2005 },
new Vehicle { VehicleID = "83-844-11", Model = "Honda Civic", Year = 2007 },
};
ObjectDumper.Write(v);
It can also print an object with inner collections, up to a given depth:
class Company
{
public Vehicle[] Vehicles { get; set; }
}
...
Vehicle[] v = new Vehicle[]
{
new Vehicle { VehicleID = "30-804-15", Model = "Mazda 3", Year = 2006 },
new Vehicle { VehicleID = "29-123-43", Model = "Subaru B4", Year = 2005 },
new Vehicle { VehicleID = "83-844-11", Model = "Honda Civic", Year = 2007 },
};
Company c = new Company { Vehicles = v };
ObjectDumper.Write(c, 0);
or when using:
ObjectDumper.Write(c, 1);
Great, now where can I grab it?
The ObjectDumper is one of the projects that ships as part of the Linq Samples. You can find the samples download page here, and after you download and install them, you can find them in the CSharpSamples.zip file in C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\ directory.
Enjoy!