C# Visual Studio 如何从调试器序列化对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18794264/
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-08-10 13:13:45  来源:igfitidea点击:

Visual Studio how to serialize object from debugger

c#debuggingserializationvisual-studio-2012

提问by xvorsx

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?

我正在尝试调查故障转储中的错误(因此我无法更改代码)。我有一个非常复杂的对象(序列化表示中有数千行)并且它的状态不一致。要调查其状态,Visual Studio 调试器视图是无用的。但是对象有一个数据契约。我想序列化它,然后使用我最喜欢的文本编辑器来浏览对象。是否可以从调试器执行?

采纳答案by Alexey

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.xmlfile to c:\temp, change it to your liking.

前段时间我写了这个单行将对象序列化到磁盘上的文件。将它复制/粘贴到您的立即窗口,并obj用您的对象替换(它被引用两次)。它会将text.xml文件保存到c:\temp,根据您的喜好进行更改。

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

不过不要指望有什么魔法,如果对象不能被序列化,它会抛出异常。

回答by Chuck Conway

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.

另一种选择是覆盖该ToString()方法并在调试模式下调用它。

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.

您还可以在崩溃前不久将内容写入文件,或者将代码包装到 try/catch 中然后写入文件。我假设您可以确定它何时崩溃。

回答by Mike Cheel

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:

我用 saveTo 的字符串重载它并从直接窗口调用它:

public static void ToSerializedObjectForDebugging(this object o, string saveTo)
{
    ToSerializedObjectForDebugging(o, new FileInfo(saveTo));
}

回答by mcintyre321

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

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

Newtonsoft.Json.JsonConvert.SerializeObject(someVariable)

Newtonsoft.Json.JsonConvert.SerializeObject(someVariable)

回答by Omar Elabd

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

这是一个 Visual Studio 扩展,它可以让你做到这一点:

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

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

You can output to JSON, XML or C#

您可以输出到 JSON、XML 或 C#

回答by Simon Tewsi

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

阿列克谢回答的变体。稍微复杂一点,但不涉及写入文本文件:

1) In the Immediate Window enter:

1) 在立即窗口输入:

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

2) In the Watch Window enter two watches:

2) 在 Watch Window 中输入两个手表:

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.

进入第二个监视(序列化监视)后,stringWriter 监视值将设置为序列化为 XML 的 obj。复制并粘贴它。请注意,XML 将括在大括号 {...} 中,因此如果您想将 XML 用于任何内容,则需要删除它们。

回答by Michael Armes

A variation on the answer from Omar Elabd --

来自 Omar Elabd 的答案的变体——

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

它不是免费的,但有 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).

在那里的上下文/悬停菜单中有内置的 JSON 导出,它比 Object Export 扩展更好用(权衡它不是免费的)。

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

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

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.

我知道这是事后几年,但我在这里留下一个答案,因为这对我有用,其他人可能会发现它很有用。

回答by InGeek

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
});

回答by DharmaTurtle

I've been using ObjectDumper.Net.

我一直在使用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.

它运行良好,尤其是当您进行实时单元测试时。当测试运行时,我可以在控制台中轻松查看变量的值,从而避免手动调试。

Thismay help if you're using XUnit.

如果您使用 XUnit,可能会有所帮助。

回答by Chris Peacock

Use this in Visual Studio's "Immediate" window, replacing c:\directory\file.jsonwith the full path to the file to which you'd like to write the JSON and myObjectwith your variable to serialise:

在 Visual Studio 的“立即”窗口中使用它,替换c:\directory\file.json为要写入 JSON 的文件的完整路径以及myObject要序列化的变量:

System.IO.File.WriteAllText(@"c:\directory\file.json", Newtonsoft.Json.JsonConvert.SerializeObject(myObject))