如何在 Visual Studio 中使用 LINQPad Dump() 扩展方法?

发布于 2024-08-30 12:40:18 字数 370 浏览 9 评论 0原文

LINQPad 令人惊叹,特别有用的是 Dump() 扩展方法,它可以将几乎任何类型(匿名与否)的对象和结构呈现到控制台。

最初,当我迁移到 Visual Studio 2010 时,我尝试使用委托创建自己的 Dump 方法来获取要呈现的匿名类型等的值。虽然它变得非常复杂,但也很有趣首先是教育性的,我需要一个坚实的实施。在查看 .NET Reflector 中的 LINQPad 代码后,我更加确信我不是将得到正确的实施。

是否有免费的库可以提供 Dump 功能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

筱果果 2024-09-06 12:40:18

我为 Object 编写了一个扩展方法,该方法使用带有漂亮格式选项的 Json.Net 序列化器。当采用这样的格式时,JSON 很容易阅读。您错过了类型信息,但我不知道您是否需要它,特别是考虑到这是多么容易。还没有让我失望。我使用 Json.Net 而不是 MS',因为它能够处理复杂图中的循环引用,而 MS' 不能,或者在我想到它时没有。

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

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;
    }
}
悲歌长辞 2024-09-06 12:40:18

看这里(您的路径可能会有所不同):

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

Look here (your path may vary):

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Samples\1033\CSharpSamples.zip\LinqSamples\ObjectDumper
她如夕阳 2024-09-06 12:40:18

diceguyd30答案 源自 讨论(尤其是 Pat Kujawa 和 anunay 的评论)并描述了如何从 C# 和 VB.NET 调用 LINQPad 转储实现:

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

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;
}
动听の歌 2024-09-06 12:40:18

还有名为 ObjectDumper 的类库作为 NuGet 包提供。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文