How to Dump Objects in C#


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

ObjectDumper is a very handy assembly that can be found under Nuget Packages.

Once installed, the assembly provides a simple single static method (Dump) that takes a Generic type T, Dump name and a stream writer as parameters.

The idea of the object dumper is that it recursively loops on the object’s properties through reflection and collects all data underlying that object.

See the below sample code, I am using ObjectDumper's dump method to output the object's types and values onto the console.

Unfortunately, the ObjectDumper Nuget package is not supported by .Net Core which this lesson is built on it, so I am posting plain code as a snippet. If you are using the regular .Net framework as your environment then you can try this code on your local development machine.

using System;
using Models;

namespace ConsoleApplication1{
	class Program
		public static void Main(string[] args)
			Item item = new Item
				Name = "Chocolate",
				Number = 12345,
				CreatedDate = DateTime.Now,
				Price = 36.7M,
				Category = new Category(1, "Sweets")
			using (var writer = new System.IO.StringWriter())
				ObjectDumper.Dumper.Dump(item, "Object Dumper", writer);

After running the above code, the output will become:

ObjectDumper Output

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content