Copy object values in Visual Studio debug mode

前端 未结 12 513
轻奢々
轻奢々 2021-01-29 19:17

In Visual Studio debug mode it\'s possible to hover over variables to show their value and then right-click to \"Copy\", \"Copy Expression\" or \"Copy Value\".

In case t

相关标签:
12条回答
  • 2021-01-29 19:20

    I always use:

    string myJsonString = JsonConvert.SerializeObject(<some object>);
    

    Then I copy the string value which unfortunately also copies the back slashes.

    To remove the backlashes go here: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_replace

    Then within the <p id="demo">Visit Microsoft!</p> element replace the text with the text you copied. then replace the var res = str.replace("Microsoft", "W3Schools"); line with

    var res = str.replace(/\\/g, '')
    

    Run these new changes but don't forget to click the "try it" button on the right.

    Now you should have all the text of the object in json format that you can drop in a json formatter like http://jsonformatter.org or to create a POCO you can now use http://json2csharp.com/

    0 讨论(0)
  • 2021-01-29 19:33

    There is a recent extension Object Exporter that does this conveniently.

    http://www.omarelabd.net/exporting-objects-from-the-visual-studio-debugger/

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

    0 讨论(0)
  • 2021-01-29 19:33

    Most popular answer from https://stackoverflow.com/a/23362097/2680660:

    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)
    
    0 讨论(0)
  • 2021-01-29 19:34

    In the immediate window, type

    ?name_of_variable

    This will print out everything, and you can manually copy that anywhere you want, or use the immediate window's logging features to automatically write it to a file.

    UPDATE: I assume you were asking how to copy/paste the nested structure of the values so that you could either search it textually, or so that you can save it on the side and then later compare the object's state to it. If I'm right, you might want to check out the commercial extension to Visual Studio that I created, called OzCode, which lets you do these thing much more easily through the "Search" and "Compare" features.

    UPDATE 2 To answer @ppumkin's question, or new EAP has a new Export feature allows users to Export the variable values to Json, XML, Excel, or C# code.

    Full disclosure: I'm the co-creator of the tool I described here.

    0 讨论(0)
  • 2021-01-29 19:37

    You can run below code in immediate window and it will export to an xml file the serialized XML representation of an object:

    (new System.Xml.Serialization.XmlSerializer(obj.GetType())).Serialize(new System.IO.StreamWriter(@"c:\temp\text.xml"), obj)
    

    Source: Visual Studio how to serialize object from debugger

    0 讨论(0)
  • 2021-01-29 19:40

    By using attributes to decorate your classes and methods you can have a specific value from your object display during debugging with the DebuggerDisplay attribute e.g.

    [DebuggerDisplay("Person - {Name} is {Age} years old")]
    public class Person
    {
      public string Name { get; set; }
      public int Age { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题