How to dump object properties in C#

gpeipman
1,372 views

Open Source Your Knowledge, Become a Contributor

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

Create Content

This example demonstrates how to dump object properties to console or some other stream using reflection. It works only on instance properties as there is rare need to dump out static part of objects.

The idea is simple:

  1. Create dictionary with properties and their values using reflection
  2. For null-values use empty string
  3. Step through dictionary and write properties and their values to given text writer

Code sample below demonstrates console application that dumps out properties of simple Person object. Be careful with object dumps when working with legacy code where properties may contain expensive code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
class Hello
{
static void Main()
{
var person = new Person {
FirstName = "Gunnar",
LastName = "Peipman",
SSN = "-i"
};
ObjectDump.Write(Console.Out, person);
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string SSN { get; set; }
}
public static 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(": ");
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

From here

It's possible to move to extension methods with code above and have Dump() method for all non-static objects. Negative side of extension method is the fact that when it is in some other namespace then developers must be aware about this namespace. But if this piece of code is needed then they will know it of course.

Here is the extension method version of code above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
class Hello
{
static void Main()
{
var person = new Person {
FirstName = "Gunnar",
LastName = "Peipman",
SSN = "-i"
};
person.Dump(Console.Out);
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string SSN { get; set; }
}
public static class ObjectExtensions
{
public static void Dump(this object obj, TextWriter writer)
{
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(": ");
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

References

Original article: How to dump object properties by Gunnar Peipman

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