Visual Studio how to serialize object from debugge

2019-01-21 01:26发布

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?

7条回答
一纸荒年 Trace。
2楼-- · 2019-01-21 01:58

A variation on Alexey's answer. Slightly more complicated but doesn't involve writing to a text file:

1) In the Immediate Window enter:

System.IO.StringWriter stringWriter = new System.IO.StringWriter();  

2) In the Watch Window enter two watches:

a.  stringWriter

b.  new System.Xml.Serialization.XmlSerializer(obj.GetType()).Serialize(stringWriter, obj) 

After you enter the second watch (the Serialize one) the stringWriter watch value will be set to obj serialized to XML. Copy and paste it. Note that the XML will be enclosed in curly braces, {...}, so you'll need to remove them if you want to use the XML for anything.

查看更多
Emotional °昔
3楼-- · 2019-01-21 02:15

Here is a Visual Studio extension which will let you do exactly that:

https://visualstudiogallery.msdn.microsoft.com/c6a21c68-f815-4895-999f-cd0885d8774f

You can output to JSON, XML or C#

查看更多
Evening l夕情丶
4楼-- · 2019-01-21 02:21

I have an extension method I use:

public static void ToSerializedObjectForDebugging(this object o, FileInfo saveTo)
{
    Type t = o.GetType();
    XmlSerializer s = new XmlSerializer(t);
    using (FileStream fs = saveTo.Create())
    {
        s.Serialize(fs, o);
    }
}

I overload it with a string for saveTo and call it from the immediate window:

public static void ToSerializedObjectForDebugging(this object o, string saveTo)
{
    ToSerializedObjectForDebugging(o, new FileInfo(saveTo));
}
查看更多
姐就是有狂的资本
5楼-- · 2019-01-21 02:22

With any luck you have Json.Net in you appdomain already. In which case pop this into your Immediate window:

Newtonsoft.Json.JsonConvert.SerializeObject(someVariable)

查看更多
甜甜的少女心
6楼-- · 2019-01-21 02:22

A variation on the answer from Omar Elabd --

It's not free, but there's a free trial for OzCode
(https://marketplace.visualstudio.com/items?itemName=CodeValueLtd.OzCode).

There's built-in exporting to JSON within the context/hover menu there, and it works a bit better than the Object Export extension (the trade-off for it not being free).

http://o.oz-code.com/features#export (demo)

I know this is a few years after the fact, but I'm leaving an answer here because this worked for me, and someone else may find it useful.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-21 02:24

Some time ago I wrote this one-liner serializing an object to a file on the disk. Copy/paste it to your Intermediate window, and replace obj (it's referenced twice) with your object. It'll save the 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 through an exception.

查看更多
登录 后发表回答