63

LINQPad is amazing, and 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 while it was fun and educational at first, I need a solid implementation. Having checked out the LINQPad code in .NET 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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gav
  • 29,022
  • 23
  • 65
  • 90
  • 13
    this question should not be closed because it did not solicit debate, arguments, polling, or extended discussion – Michael Freidgeim Aug 17 '16 at 13:53
  • I see the dump method in linqpad from here:--- https://www.linqpad.net, --- http://i.imgur.com/7j2WdZw.png and I think it's a really special method that used in linqpad, so I don't think there's easy way to do it in real C#. as its decription it could auto turn img to img, calendar to calendar, there must be lots of object type decision in real code behind – yu yang Jian Nov 26 '16 at 15:41
  • and I take a look at the ObjectDumper, it use the Write method in the end which is using TextWriter class, so I think what it do is simply write all the object to string maybe not do what you want. – yu yang Jian Nov 26 '16 at 15:46
  • Did you look here already? https://stackoverflow.com/questions/5414214/linqpad-in-visual-studio/38199055#38199055 – Matt Oct 27 '17 at 11:48

4 Answers4

65

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);
    }

    public static T Dump<T>(this T value)
    {
        Console.WriteLine(value.ToPrettyString());
        return value;
    }
}
UnionP
  • 1,251
  • 13
  • 26
Chad Ruppert
  • 3,650
  • 1
  • 19
  • 19
  • To further emulate LINQPad's Dump() method, add this extension method to the class: public static T Dump(this T value) { Console.WriteLine(value.ToPrettyString()); return value; } – UnionP Apr 23 '20 at 20:12
  • 1
    If anyone is adapting this to use `System.Text.Json` despite the reasoning in this post, the only change needed besides class/object names is to pass `new JsonSerializerOptions(JsonSerializerDefaults.Web) { WriteIndented = true };` instead of `Formatting.Indented` to the `Serialize` method. `WriteIndented = true` will do similar formatting to pretty-print the resulting string. – TheAtomicOption Oct 28 '21 at 17:33
37

Look here (your path may vary):

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Samples\1033\CSharpSamples.zip\LinqSamples\ObjectDumper
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Raj Kaimal
  • 8,304
  • 27
  • 18
  • 1
    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 Apr 23 '10 at 15:16
13

diceguyd30's answer is sourced from a discussion (especially Pat Kujawa's & anunay's comments) and describes how to call the LINQPad dump implementation from both C# and VB.NET:

public static string DumpToHtmlString<T>(this T objectToSerialize)
{
    string strHTML = "";
    try
    {
        var writer = LINQPad.Util.CreateXhtmlWriter(true);
        writer.Write(objectToSerialize);
        strHTML = writer.ToString();
    }
    catch (Exception exc)
    {
        Debug.Assert(false, "Investigate why ?" + exc);
    }
    return strHTML;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
11

There's also a class library named ObjectDumper available as a NuGet package.

Igor Pashchuk
  • 2,455
  • 2
  • 22
  • 29
Carl Tonander
  • 111
  • 1
  • 3