visual-studio 在 Visual Studio 调试模式下复制对象值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2927991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:02:57  来源:igfitidea点击:

Copy object values in Visual Studio debug mode

visual-studiovisual-studio-2010debugging

提问by Farinha

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".

在 Visual Studio 调试模式下,可以将鼠标悬停在变量上以显示其值,然后右键单击“复制”、“复制表达式”或“复制值”。

In case the variable is an object and not just a basic type, there's a + sign to expand and explore the object. It there a way to copy all that into the clipboard?

如果变量是一个对象而不仅仅是一个基本类型,则有一个 + 号来扩展和探索该对象。有没有办法将所有内容复制到剪贴板中?

回答by Omer Raviv

In the immediate window, type

在即时窗口中,键入

?name_of_variable

?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.

更新:我假设您问的是如何复制/粘贴值的嵌套结构,以便您可以以文本方式搜索它,或者可以将其保存在一边,然后再将对象的状态与它进行比较。如果我是对的,您可能想查看我创建的 Visual Studio 商业扩展,称为OzCode,它可以让您通过“搜索”和“比较”功能更轻松地执行这些操作。

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

更新 2回答@ppumkin 的问题,或者新 EAP有一个新的导出功能,允许用户将变量值导出到 Json、XML、Excel 或 C# 代码。

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

完全披露:我是我在这里描述的工具的共同创建者。

回答by Bat_Programmer

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

您可以在立即窗口中运行下面的代码,它会将对象的序列化 XML 表示导出到 xml 文件:

(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

来源:Visual Studio 如何从调试器序列化对象

回答by PMN

You can add a watch for that object, and in the watch window, expand and select everything you want to copy and then copy it.

您可以为该对象添加监视,并在监视窗口中展开并选择要复制的所有内容,然后复制它。

回答by Efreeto

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

来自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)

幸运的是,您的 appdomain 中已经有了 Json.Net。在这种情况下,将其弹出到您的立即窗口中:

Newtonsoft.Json.JsonConvert.SerializeObject(someVariable)

回答by Dave Anderson

By using attributes to decorate your classes and methodsyou can have a specific value from your object display during debugging with the DebuggerDisplay attributee.g.

通过使用属性来装饰您的类和方法,您可以在调试期间使用DebuggerDisplay 属性从对象显示中获得特定值,例如

[DebuggerDisplay("Person - {Name} is {Age} years old")]
public class Person
{
  public string Name { get; set; }
  public int Age { get; set; }
}

回答by Jesse Hufstetler

Google led me to this 8-year-old question and I ended up using ObjectDumperto achieve something very similar to copy-pasting debugger data. It was a breeze.

谷歌把我引向了这个 8 年前的问题,我最终使用ObjectDumper来实现与复制粘贴调试器数据非常相似的东西。这是微风。

I know the question asked specifically about information from the debugger, but ObjectDumper gives information that is basically the same. I'm assuming those who google this question are like me and just need the data for debugging purposes and don't care whether it technically comes from the debugger or not.

我知道专门针对来自debugger 的信息提出的问题,但 ObjectDumper 提供的信息基本相同。我假设那些在谷歌上搜索这个问题的人和我一样,只需要用于调试目的的数据,而不关心它在技术上是否来自调试器。

回答by Helzgate

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

要删除强烈反对,请访问: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

然后在<p id="demo">Visit Microsoft!</p>元素内用您复制的文本替换文本。然后var res = str.replace("Microsoft", "W3Schools");

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.orgor to create a POCO you can now use http://json2csharp.com/

现在您应该拥有 json 格式的对象的所有文本,您可以将其放入像http://jsonformatter.org这样的 json 格式化程序中,或者创建一个 POCO,您现在可以使用http://json2csharp.com/

回答by Marcus Parsons

I know I'm a bit late to the party, but I wrote a JSON implementation for serializing an object, if you prefer to have JSON output. Uses Newtonsoft.Json reference.

我知道我参加聚会有点晚了,但我写了一个 JSON 实现来序列化一个对象,如果你更喜欢有 JSON 输出。使用 Newtonsoft.Json 参考。

private static void WriteDebugJSON (dynamic obj, string filePath)
{
    using (StreamWriter d = new StreamWriter(filePath))
    {
        d.Write(JsonConvert.SerializeObject(obj));
    }
}

回答by emert117

if you have a list and you want to find a specific variable: In the immediate window, type

如果您有一个列表并且想要查找特定变量:在即时窗口中,键入

 myList.Any(s => s.ID == 5062);

if this returns true

如果这返回真

var myDebugVar = myList.FirstOrDefault(s => s.ID == 5062);
?myDebugVar