LINQPad in Visual Studio
Asked Answered
B

3

27
public static class Extensions{
public static void Dump<T>(this T o) { }
public static void Dump<T>(this T o, string s) { }}

These lines allow me to copy code from LINQPad to VS and run it without commenting out every line with .Dump() but it's not enough... http://code.google.com/p/linqpadvisualizer/ - not very comfortable :(

The best result I get searching for LINQPad in VS is this site with code below by Pat Kujawa.

using System.Diagnostics;
using System.IO;
public static class Extensions
{
public static void Dump<T>(this T o)
{
    string localUrl = Path.GetTempFileName() + ".html";
    using (var writer = LINQPad.Util.CreateXhtmlWriter(true))
    {
        writer.Write(o);
        File.WriteAllText(localUrl, writer.ToString());
    }
    Process.Start(localUrl);
}
}

but Error 1 The name 'LINQPad' does not exist in the current context

I couldn't find any LINQPad.dll on the net

Bukavu answered 24/3, 2011 at 2:50 Comment(0)
D
35

It's not a dll for LINQPad - you need to reference the LINQPad.exe itself.

Right-click your project in Visual Studio -> Add Reference -> Browse to the exe binary file location, typically found in its install directory C:\Program Files\LINQPad\ --> select LINQPad.exe.

Once done, then you can add a "using directive" for it in your file:

using System.Diagnostics;
using System.IO;
using LINQPad;

The method LINQPad.Util.CreateXhtmlWriter will now be available for you to use.

Damron answered 24/3, 2011 at 3:11 Comment(2)
Had no idea you could reference .exe files. Thank you.Sienkiewicz
@Przemyslaw An assembly is an assembly.Winna
I
22

In addition to the answers given above, I found a simple solution to do "in place" debugging inside Visual Studio (2015).


Preparation

  1. As Ray Vega wrote, add a reference to the x86 version (remember Visual Studio is still not 64 bit!) of LinqPad (i.e. Add Reference -> Browse to the exe binary file location typically found in its install directory C:\Program Files\LINQPad\ -> select LINQPad.exe.)

  2. In the scope where you want to use dump, add:

    public static dynamic dump = LINQPad.Util.CreateXhtmlWriter();
    
  3. To dump, add to your code where you require a dump:

    dump.Write(obj); // obj = the object to dump
    
  4. Add breakpoints where required.

Note: If you require compatibility with the LinqPad .Dump() method, declare the following instead of steps 2. and 3.:

public static class DumpExtension
{
    private static dynamic dump = LINQPad.Util.CreateXhtmlWriter();
    public static T Dump<T>(this T objToDump)
    {
        dump.Write(objToDump);
        return objToDump;
    }
}

In this case, place the breakpoint in the line where the return objToDump statement is.


Visualisation

In the watch window, add

    dump.ToString()

Click on the spyglass icon and select "HTML Visualizer". HtmlVisualizer

When a breakpoint is hit, you can click on the spyglass and in the popup window opening you can see the rendered dump (just as you would see it in LinqPad).

DumpExample

In this example, the expression

        dump.Write(new string[] { "a", "b" });

or (if you prefer the other syntax using the extension method mentioned above)

        (new string[] { "a", "b" }).Dump();

was rendered.

Note that

  • because we're using dynamic, sometimes it is required to explicitly add Microsoft.CSharp to the project's references or you will get an error message. See discussion here.
  • you need to use .NET Framework 4.5.2 or higher, lower framework versions will not work
  • like in LinqPad, everything you dump will be appended.
  • you should use this in unit tests only, not in production code, because when you deploy your application the dump statements are still there. Of course, you can surround all dump statements (including the statement from step 2. in the preparation section) by #if statements like:

    #if DEBUG dump.Write(new string[] { "a", "b" }); #endif

    In case you want to bind the LinqPad reference to the DEBUG configuration only, you can find a hint here (or in more detail there) how you can achieve that.

Illusory answered 5/7, 2016 at 8:50 Comment(0)
V
4

Recommend to use LINQPad related nuget package, then you can use Dump() method with it.

LINQPad

For .NET core:

  • Install LINQPad.Runtime

For .NET framework 4 etc.

  • Install LINQPad
Viridescent answered 20/1, 2021 at 4:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.