本文介绍了有没有提供像LinqPad这样的格式化Dump()函数的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中处理了很多Linq查询,并且正在寻找一个提供与LinqPad提供的功能类似的格式化Dump()函数的库. LinqPad的Dump()扩展方法确实非常不错,因为它可以很好地处理嵌套集合.

I work with a lot of Linq queries in my code, and I'm looking for a library that provides a formatted Dump() function similar to what LinqPad offers. LinqPad's Dump() extension method is really quite nice, because it handles nested collections very well.

理想情况下,它将以纯文本格式打印出漂亮的表格,但是我可以吐出HTML或其他格式良好的数据.

Ideally, it would print out pretty tables in plain text, but I'd be ok with spitting out HTML or other nicely formatted data.

VS的ObjectDumper样本根本不会剪切.

The ObjectDumper sample from VS does not cut it at all.

推荐答案

这就是我一直在使用的:

This is what I've been using:

特别感谢此主题(尤其是Pat Kujawa的& anunay的评论)

C#(直接摘自Pat Kujawa的评论(尽管我使它返回了自己,以便它像linqpad的版本一样进行链接)):

C# (Straight from Pat Kujawa's comment (though I made it return itself so that it chains like linqpad's version does)):

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

VB(由于我在VB应用中需要它,所以我进行了转换):

VB (my conversion since I needed it in a VB app):

Public Module LinqDebugging
    <System.Runtime.CompilerServices.Extension()>
    Public Function Dump(Of T)(ByVal o As T) As T
        Dim localUrl = Path.GetTempFileName() + ".html"
        Using writer = LINQPad.Util.CreateXhtmlWriter(True)
            writer.Write(o)
            File.WriteAllText(localUrl, writer.ToString())
        End Using
        Process.Start(localUrl)
        Return o
    End Function
End Module

您将需要在项目中添加linqpad可执行文件作为参考以及System.IOSystem.Diagnostics

这会启动您的默认Web浏览器,显示linqpad将生成的确切输出.

This launches your default web browser showing the exact output that linqpad would generate.

这篇关于有没有提供像LinqPad这样的格式化Dump()函数的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 06:12