Visual Studio如何从调试器序列化对象 [英] Visual Studio how to serialize object from debugger
问题描述
我正在尝试调查故障转储中的错误(因此我无法更改代码)。
我有一个非常复杂的对象(序列化表示中的数千行),并且其状态不一致。要调查其状态,Visual Studio调试器视图是无用的。但是对象具有数据合同。我想序列化它,然后使用我最喜欢的文本编辑器在对象上导航。
I'm trying to investigate a bug in a crash dump (so I can not change the code). I have a really complicated object (thousands of lines in the serialized representation) and its state is inconsistent. To investigate its state the Visual Studio debugger view is useless. But the object has a data contract. I'd like to serialize it and then use my favorite text editor to navigate across the object. Is it possible to do the from the debugger?
推荐答案
前段时间,我写了这篇单行代码,将对象序列化为文件在磁盘上。将其复制/粘贴到即时窗口中,并用对象替换 obj
(两次被引用)。它将将 text.xml
文件保存到 c:\temp
,并根据您的喜好进行更改。
Some time ago I wrote this one-liner serializing an object to a file on the disk. Copy/paste it to your Immediate window, and replace obj
(it's referenced twice) with your object. It'll save a text.xml
file to c:\temp
, change it to your liking.
(new System.Xml.Serialization.XmlSerializer(obj.GetType())).Serialize(new System.IO.StreamWriter(@"c:\temp\text.xml"), obj)
不要期望任何神奇的是,如果对象无法序列化,它将抛出异常。
Don't expect any magic though, if the object cannot be serialized, it'll throw an exception.
这篇关于Visual Studio如何从调试器序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!