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 can find the ObjectDumper project on CodePlex. You can also add it via Visual Studio 2010 Nuget package manager.
Here is an alternative:
(just touching level 1, no depth, but says a lot)
Here's a visual studio extension I wrote to do this:
https://visualstudiogallery.msdn.microsoft.com/c6a21c68-f815-4895-999f-cd0885d8774f
in action:
If you're working with markup,
System.Web.ObjectInfo.Print
(ASP.NET Web Pages 2) will accomplish this, nicely formatted for HTML.For example:
In a webpage, produces:
I have a handy T.Dump() Extension method that should be pretty close to the results you're looking for. As its an extension method, its non-invasive and should work on all POCO objects.
Example Usage
Example Output