对象转储器类
我正在寻找一个可以以类似于此的格式输出对象及其所有叶值的类:(
User
- Name: Gordon
- Age : 60
- WorkAddress
- Street: 10 Downing Street
- Town: London
- Country: UK
- HomeAddresses[0]
...
- HomeAddresses[1]
...
或更清晰的格式)。这相当于:
public class User
{
public string Name { get;set; }
public int Age { get;set; }
public Address WorkAddress { get;set; }
public List<Address> HomeAddresses { get;set; }
}
public class Address
{
public string Street { get;set; }
public string Town { get;set; }
public string Country { get;set; }
}
PropertyGrid 控件的一种字符串表示形式,无需为每种类型实现大量设计器。
PHP 有一个叫做 var_dump 的东西可以做到这一点。我不想使用手表,因为这是用于打印的。
如果存在的话,有人可以向我指出这样的事情吗?或者,写一篇以获得赏金。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
sgmoore 链接中发布的对象转储程序:
2015 Update
完成此操作的方法
YAML 也很好地满足了此目的,这就是使用 YamlDotNet
install-package YamlDotNet
The object dumper posted in sgmoore's link:
2015 Update
YAML also serves this purpose quite well, this is how it can be done with YamlDotNet
install-package YamlDotNet
您可以使用 JSON 序列化程序,对于任何使用 JSON 的人来说都应该很容易阅读
You could use the JSON serialiser, which should be easy to read for anyone use to working with JSON
2019 年更新
您可以在 GitHub 上找到 ObjectDumper 项目。您还可以通过 NuGet 包管理器通过 Visual Studio 添加它。
Updated 2019
You can find the ObjectDumper project on GitHub. You can also add it via Visual Studio via NuGet package manager.
如果您使用标记,
System.Web.ObjectInfo.Print
(ASP.NET Web Pages 2)将完成此任务,并针对 HTML 进行了良好的格式化。例如:
在网页中,生成:
If you're working with markup,
System.Web.ObjectInfo.Print
(ASP.NET Web Pages 2) will accomplish this, nicely formatted for HTML.For example:
In a webpage, produces:
这是我为此编写的一个 Visual Studio 扩展:
https://visualstudiogallery。 msdn.microsoft.com/c6a21c68-f815-4895-999f-cd0885d8774f
实际操作:

Here's a visual studio extension I wrote to do this:
https://visualstudiogallery.msdn.microsoft.com/c6a21c68-f815-4895-999f-cd0885d8774f
in action:

我知道这是一个老问题,但我想我应该放弃一个对我有用的替代方案,花了我大约两分钟的时间。
安装Newtonsoft Json.NET:
http://james.newtonking.com/json
(或 nuget 版本)http://www.nuget.org/packages/newtonsoft.json/
参考程序集:
转储 JSON 字符串到日志:
I know this is an old question, but thought I'd throw out an alternative that worked for me, took me about two minutes to do.
Install Newtonsoft Json.NET:
http://james.newtonking.com/json
(or nuget version) http://www.nuget.org/packages/newtonsoft.json/
Reference Assembly:
Dump JSON string to log:
只要稍加思考,你就可以很容易地写出它。类似于:
您可以编写 PrintArray 和 PrintProperty 方法。
You could write that very easily with a little bit of reflection. Something kind of like:
You can write up the PrintArray and PrintProperty methods.
我有一个 方便的 T.Dump() 扩展方法,它应该非常接近您正在寻找的结果。作为一种扩展方法,它是非侵入性的,并且应该适用于所有 POCO 对象。
用法示例
输出示例
I have a handy T.Dump() Extension method that should be pretty close to the results you're looking for. As its an extension method, its non-invasive and should work on all POCO objects.
Example Usage
Example Output
如果您不想复制和粘贴 Chris S 的代码,Visual Studio 2008 示例附带了一个 ObjectDumper。
驱动器:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\LinqSamples\ObjectDumper
If you don't feel like copying and pasting Chris S's code, the Visual Studio 2008 samples come with an ObjectDumper.
Drive:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\LinqSamples\ObjectDumper
这是一个替代方案:(
只是触及第 1 级,没有深度,但说了很多)
Here is an alternative:
(just touching level 1, no depth, but says a lot)
对于大多数类,您可以使用 DataContractSerializer
For most classes, you could use the DataContractSerializer
我刚刚在 Blazor 项目中遇到了类似的要求,并提出了以下非常简单的组件来将对象(及其子对象)的数据输出到屏幕:
ObjectDumper.razor:
然后将其粘贴到 Blazor 页面上的某个位置如下所示:
然后呈现一个按钮,您可以按下该按钮来查看绑定对象的当前值:
我已将其粘贴在 GitHub 存储库中:tomRedox/BlazorObjectDumper
I just came across a similar requirement in a Blazor project, and came up with the following very simple component to output an object's (and it's child objects') data to the screen:
ObjectDumper.razor:
You then stick that somewhere on a Blazor page as follows:
Which then renders a button you can press to see the current values of the bound object:
I've stuck that in a GitHub repo: tomRedox/BlazorObjectDumper