tags:

views:

343

answers:

2

Hi,

LINQPad is amazing, particularly useful is the Dump() extension methods which renders objects and structs of almost any type, anonymous or not, to the console.

Initially, when I moved to Visual Studio 2010, I tried to make my own Dump method using a delegate to get the values to render for anonymous types etc. It's getting pretty complicated though and whilst it was fun and educational at first what I need is a solid implementation. Having checked out the LinqPad code in reflector I am even more assured that I'm not going to get the implementation right.

Is there a free library I can include to provide the Dump functionality?

Thanks,

Gavin

+5  A: 

Look here (your path may vary):

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Samples\1033\CSharpSamples.zip\LinqSamples\ObjectDumper

Raj Kaimal
+1 Very handy class. I never knew about that. I'm going to copy it into my debugging tools library. Thanks
Simon P Stevens
I compiled it and added as a reference to my project but I get an error trying to build - ObjectDumper does not exist in current context. How would you call it? Is Write an extension method or just a static. Sorry, I'm new to C#.
gav
+1  A: 

I wrote an extension method to Object that uses the Json.Net serializer with the pretty format option. JSON is easy enough to read when formatted like that. You miss type info, but I don't know that you need that, especially considering how easy this is. Hasn't failed me yet. I use Json.Net and not MS' because it has the capability of handling circular references in complex graphs, where MS' cannot, or didn't at the time I thought of it.

    using Newtonsoft.Json;
    public static class Dumper{
        public static string ToPrettyString(this object value)
        {
             return JsonConvert.SerializeObject(value, Formatting.Indented);
        }
    }
Chad Ruppert