I'm looking for a class that can output an object and all its leaf values in a format similar to this:
User
- Name: Gordon
- Age : 60
- WorkAddress
- Street: 10 Downing Street
- Town: London
- Country: UK
- HomeAddresses[0]
...
- HomeAddresses[1]
...
(Or a clearer format). This would be equivalent to:
public class User
{
public string Name { get;set; }
public int Age { get;set; }
public Address WorkAddress { get;set; }
public List<Address> HomeAddresses { get;set; }
}
public class Address
{
public string Street { get;set; }
public string Town { get;set; }
public string Country { get;set; }
}
A kind of string representation of the PropertyGrid control, minus having to implement a large set of designers for each type.
PHP has something that does this called var_dump. I don't want to use a watch, as this is for printing out.
Could anyone point me to something like this if it exists? Or, write one for a bounty.
You could write that very easily with a little bit of reflection. Something kind of like:
You can write up the PrintArray and PrintProperty methods.
The object dumper posted in sgmoore's link:
2015 Update
YAML also serves this purpose quite well, this is how it can be done with YamlDotNet
install-package YamlDotNet
I know this is an old question, but thought I'd throw out an alternative that worked for me, took me about two minutes to do.
Install Newtonsoft Json.NET: http://james.newtonking.com/json
(or nuget version) http://www.nuget.org/packages/newtonsoft.json/
Reference Assembly:
Dump JSON string to log:
If you don't feel like copying and pasting Chris S's code, the Visual Studio 2008 samples come with an ObjectDumper.
Drive:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\LinqSamples\ObjectDumper
You could use the JSON serialiser, which should be easy to read for anyone use to working with JSON
For most classes, you could use the DataContractSerializer