George Mauer
George Mauer

Reputation: 122202

What is the .NET equivalent of PHP var_dump?

I remember seeing a while ago that there is some method in maybe the Reflection namespace that would recursively run ToString() on all of an object's properties and format it nicely for display.

Yes, I know everything I could want will be accessible through the debugger, but I'm wondering if anyone knows that command?

Upvotes: 15

Views: 24215

Answers (5)

Tomasz Swider
Tomasz Swider

Reputation: 2382

You can write it by yourself. For example, serialize it into json using Newtonsoft's JSON.net library and write that json to console. Here is an example:

using Newtonsoft.Json;

static class Pretty
{
    public static void Print<T> (T x)
    {
        string json = JsonConvert.SerializeObject(x, Formatting.Indented);
        Console.WriteLine(json);
    }
}

Usage:

Pretty.Print(whatever);

Upvotes: 3

user13810
user13810

Reputation: 108

I think what you're looking for is/was called ObjectDumper. It uses reflection to iterate through and output all of the different properties for an object. I first heard about it while learning LINQ, and most of the examples in the Linq in Action book use it.

It appears that Microsoft didn't include it in the final version of Linq though, but the code is still out in the wild. I did a quick google search for it and here's a link to it:

ObjectDumper Source Code

Upvotes: 8

sontek
sontek

Reputation: 12451

Here is a link with code dumper and a demo project that shows you how to use it. Download it here.

Upvotes: 0

Martin
Martin

Reputation: 40365

Example code to dump an object and its properties can be found here:

http://www.developer.com/net/csharp/article.php/3713886

Upvotes: 4

cori
cori

Reputation: 8830

I could certainly see the use in such a thing, but in .Net won't you mostly just get a list of type names (String, Array, etc)? Most of the built-ins don't have "useful" ToString() overloads pre-written, do they?

Upvotes: 0

Related Questions