Visual Studio how to serialize object from debugger

后端 未结 10 1137
轻奢々
轻奢々 2020-12-22 16:39

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

相关标签:
10条回答
  • 2020-12-22 17:37

    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.

    0 讨论(0)
  • 2020-12-22 17:37

    It might be possible to use the immediate window to serialize it and then copy the content to your favorite editor.

    Another option is to override the ToString() method and call it while in debug mode.

    You can also write the contents out to a file shortly before the crash, or wrap the code into a try/catch and write the file then. I'm assuming you can identify when it's crashing.

    0 讨论(0)
  • 2020-12-22 17:37

    I've been using ObjectDumper.Net.

    It works well, especially if you have live unit testing. I can easily view a variable's value in the console when a test runs, saving me from debugging manually.

    This may help if you're using XUnit.

    0 讨论(0)
  • 2020-12-22 17:43

    In case you have a circular reference, run this in the immediate Window:

    Newtonsoft.Json.JsonConvert.SerializeObject(app, Newtonsoft.Json.Formatting.Indented,
    new Newtonsoft.Json.JsonSerializerSettings
    {
        ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize
    });
    
    0 讨论(0)
提交回复
热议问题