序列化&调试时打印对象的整个状态
在调试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用反射来获取列表类类型上的所有属性和字段,然后使用它来获取每个属性/值的运行时值并将它们吐到控制台。
PropertyInfo
类型 (此处)和FieldInfo
类型(此处)是您需要从您自己的类实例的Type
对象中获取的内容。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) andFieldInfo
type (here) are what you need to get from theType
object for your own class instance.反思确实是你最好的选择。您可以从根对象开始,获取其所有属性及其值,并在必要时递归地从这些值中获取属性和值。这是一项非常强大的技术,如果您还不知道它,您可能应该学习它,无论如何,这都是一个完美的学习项目。 :)
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. :)