PHP var_dump 的 .NET 等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/93699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:56:49  来源:igfitidea点击:

What is the .NET equivalent of PHP var_dump?

.netreflection

提问by George Mauer

I remember seeing a while ago that there is some method in maybe the Reflection namespace that would recursively run ToString()on all of an object's properties and format it nicely for display.

我记得不久前看到在 Reflection 命名空间中可能有一些方法可以递归地ToString()在对象的所有属性上运行并很好地格式化以供显示。

Yes, I know everything I could want will be accessible through the debugger, but I'm wondering if anyone knows that command?

是的,我知道可以通过调试器访问我想要的一切,但我想知道是否有人知道该命令?

采纳答案by user13810

I think what you're looking for is/was called ObjectDumper. It uses reflection to iterate through and output all of the different properties for an object. I first heard about it while learning LINQ, and most of the examples in the Linq in Action book use it.

我认为您正在寻找的是/被称为 ObjectDumper。它使用反射来迭代并输出对象的所有不同属性。我在学习 LINQ 时第一次听说它,Linq in Action 书中的大多数示例都使用它。

It appears that Microsoft didn't include it in the final version of Linq though, but the code is still out in the wild. I did a quick google search for it and here's a link to it:

不过,微软似乎并未将其包含在 Linq 的最终版本中,但该代码仍在野外。我在谷歌上快速搜索了它,这是它的链接:

ObjectDumper Source Code

ObjectDumper 源代码

回答by Martin

Example code to dump an object and its properties can be found here:

可以在此处找到转储对象及其属性的示例代码:

http://www.developer.com/net/csharp/article.php/3713886

http://www.developer.com/net/csharp/article.php/3713886

回答by Tomasz Swider

You can write it by yourself. For example, serialize it into json using Newtonsoft's JSON.net library and write that json to console. Here is an example:

你可以自己写。例如,使用 Newtonsoft 的 JSON.net 库将其序列化为 json 并将该 json 写入控制台。下面是一个例子:

using Newtonsoft.Json;

static class Pretty
{
    public static void Print<T> (T x)
    {
        string json = JsonConvert.SerializeObject(x, Formatting.Indented);
        Console.WriteLine(json);
    }
}

Usage:

用法:

Pretty.Print(whatever);

回答by sontek

Here is a link with code dumper and a demo project that shows you how to use it. Download it here.

这是一个包含代码转储程序的链接和一个演示项目,向您展示如何使用它。在这里下载

回答by cori

I could certainly see the use in such a thing, but in .Net won't you mostly just get a list of type names (String, Array, etc)? Most of the built-ins don't have "useful" ToString() overloads pre-written, do they?

我当然可以看到在这样的事情中的用途,但是在 .Net 中,您不会主要获得类型名称列表(字符串、数组等)吗?大多数内置函数没有预先编写“有用的” ToString() 重载,是吗?