序列化&调试时打印对象的整个状态

发布于 2024-08-21 04:17:36 字数 205 浏览 10 评论 0原文

在调试 ASP.NET 应用程序时,我想要打印出一个非常大的对象的整个状态。我希望该对象中的所有属性和值以及每个对象属性都相同,递归地。

由于应用程序的前端在显着延迟后超时,因此我无法添加监视或使用立即窗口或将鼠标悬停在对象上,因为没有足够的时间来完全检查该对象。

有没有办法在调试模式下获得对象的完整打印输出,或者说,有一个实用程序或 C# 函数可以做到这一点?

While debugging an ASP.NET application, I want to get a print-out of the entire state of a very large object. I want all the properties and values in that object and the same for every object-property, recursively.

Because the front-end of the application times out after a significant delay, I can't add a watch or use the Immediate window or hover over the object, since there won't be adequite time to fully examine the object.

Is there a way of getting a complete printout of an object in debug mode, or say, a utility or a C# function that would do this?

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

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

发布评论

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

评论(2

小巷里的女流氓 2024-08-28 04:17:36

您可以使用反射来获取列表类类型上的所有属性和字段,然后使用它来获取每个属性/值的运行时值并将它们吐到控制台。

PropertyInfo 类型 (此处)和 FieldInfo 类型(此处)是您需要从您自己的类实例的 Type 对象中获取的内容。

MyObject myObject = ... //setup my object
Type myType = myObject.GetType(); //or Type.GetType(myObject); //I think

PropertyInfo[] properties = myType.GetProperties();
FieldInfo[] fields = myType.GetFields();

properties[0].GetValue(myObject); //returns the value as an Object, so you may need to cast it afterwards.

You could use reflection to get the list of all properties and fields on the class type, then use that to get the runtime values of each of those properties / values and spit them to the console.

The PropertyInfo type (here) and FieldInfo type (here) are what you need to get from the Type object for your own class instance.

MyObject myObject = ... //setup my object
Type myType = myObject.GetType(); //or Type.GetType(myObject); //I think

PropertyInfo[] properties = myType.GetProperties();
FieldInfo[] fields = myType.GetFields();

properties[0].GetValue(myObject); //returns the value as an Object, so you may need to cast it afterwards.
小傻瓜 2024-08-28 04:17:36

反思确实是你最好的选择。您可以从根对象开始,获取其所有属性及其值,并在必要时递归地从这些值中获取属性和值。这是一项非常强大的技术,如果您还不知道它,您可能应该学习它,无论如何,这都是一个完美的学习项目。 :)

Reflection really is your best bet here. You can start with your root object, get all of its properties and their values, and if necessary get the properties and values off of those values recursively. It's a really powerful technique, and if you don't know it yet you probably should learn it anyway, and this makes a perfect project to learn with. :)

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