Visual Studio 中的 LINQPad
public static class Extensions{
public static void Dump<T>(this T o) { }
public static void Dump<T>(this T o, string s) { }}
这些行允许我将代码从 LINQPad 复制到 VS 并运行它,而无需使用 .Dump() 注释掉每一行,但这还不够...... http://code.google.com/p/linqpadvisualizer/ - 不太舒服:(
我在 VS 中搜索 LINQPad 得到的最佳结果是这个 站点 代码如下,由 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);
}
}
但错误 1 当前上下文中不存在名称“LINQPad”,
我找不到任何名称。网上的 LINQPad.dll
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它不是 LINQPad 的 dll - 您需要引用 LINQPad.exe 本身。
在 Visual Studio 中右键单击您的项目 ->添加参考->浏览到 exe 二进制文件位置,通常位于其安装目录
C:\Program Files\LINQPad\
-->选择LINQPad.exe
。完成后,您可以在文件中为其添加“using 指令”:
方法
LINQPad.Util.CreateXhtmlWriter
现在可供您使用。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\
--> selectLINQPad.exe
.Once done, then you can add a "using directive" for it in your file:
The method
LINQPad.Util.CreateXhtmlWriter
will now be available for you to use.除了上面给出的答案之外,我还找到了一个在 Visual Studio (2015) 内进行“就地”调试的简单解决方案。
准备工作
正如 Ray Vega 所写,添加对 LinqPad 的 x86 版本(记住 Visual Studio 还不是 64 位!)的引用(即 添加引用 -> 浏览到通常在其安装目录 C:\Program Files\LINQPad\ 中找到的 exe 二进制文件位置 -> 选择 LINQPad.exe。)
在要使用转储的范围中,添加:
要转储,请在需要转储的代码中添加:
在需要的位置添加断点。
注意:如果您需要与 LinqPad .Dump() 方法兼容,请声明以下内容而不是步骤 2. 和 3.:
在这种情况下,请将断点放置在
return objToDump
语句所在的行。可视化
在监视窗口中,添加
单击小望远镜图标并选择“HTML Visualizer”。
当断点被击中时,您可以单击望远镜,在弹出窗口中您可以看到渲染的转储(就像您在 LinqPad 中看到的那样)。
表达式
or (如果您更喜欢使用上述扩展方法的其他语法)
在此示例中,呈现了
。 请注意,
动态
,有时需要将Microsoft.CSharp
显式添加到项目的引用中,否则您会收到错误消息信息。请参阅此处的讨论。您应该仅在单元测试中使用它,而不是在生产代码中,因为当您部署应用程序时,转储语句仍然存在。当然,您可以用
#if
语句包围所有转储语句(包括准备部分中步骤 2. 中的语句),例如:#if DEBUG
dump.Write(new string[] { "a", "b" });
#endif
如果您只想将 LinqPad 引用绑定到 DEBUG 配置,您可以在此处找到提示(或者更详细那里)如何实现这一点。
In addition to the answers given above, I found a simple solution to do "in place" debugging inside Visual Studio (2015).
Preparation
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.)
In the scope where you want to use dump, add:
To dump, add to your code where you require a dump:
Add breakpoints where required.
Note: If you require compatibility with the LinqPad .Dump() method, declare the following instead of steps 2. and 3.:
In this case, place the breakpoint in the line where the
return objToDump
statement is.Visualisation
In the watch window, add
Click on the spyglass icon and select "HTML Visualizer".
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).
In this example, the expression
or (if you prefer the other syntax using the extension method mentioned above)
was rendered.
Note that
dynamic
, sometimes it is required to explicitly addMicrosoft.CSharp
to the project's references or you will get an error message. See discussion here.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.
推荐使用LINQPad相关的nuget包,然后就可以使用
Dump()
方法了。对于 .NET core:
对于 .NET Framework 4 等。
Recommend to use LINQPad related nuget package, then you can use
Dump()
method with it.For .NET core:
For .NET framework 4 etc.