调试器可视化器生成对象初始化器代码 [英] Debugger Visualizer to generate Object Initializer code

查看:112
本文介绍了调试器可视化器生成对象初始化器代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个要修复的错误,并且像任何其他出色的TDD实践者一样,我想编写一个失败的测试来首先代表该错误。该错误位于采用相当复杂的类型作为输入的方法中。仅当复杂类型设置了属性值的特定组合时,才会重现该错误。

We have a bug to fix, and like any good TDD practitioner, I want to write a failing test to represent the bug first. The bug is in a method that takes a rather complex type as input. The bug will only reproduce when the complex type has a certain combination of property values set.

到目前为止,我已经重现了该错误,并且可以在调试器中查看运行情况类型的时间值。现在,我需要在单元测试的安排部分中创建该复杂类型,以便可以将其输入单元测试的行为部分中的错误方法。

So far I have reproduced the bug and, in the debugger, can view the run-time value of the complex type. Now I need to create that complex type in the "Arrange" section of my unit test so that I can feed it to the buggy method in the "Act" section of the unit test.

我可以手工编写一个大对象初始化程序代码块,例如以下代码:

I can write a big object initializer code block, by hand, such as the following one:

var cats =
    new List<Cat>
        {
            new Cat {Name = "Sylvester", Age = 8},
            new Cat {Name = "Whiskers", Age = 2}
        };

甚至是类似的东西:

var cats = new List<Cat>();
var cat1 = new Cat();
cat1.Name = "Sylvester";
cat1.Age = 8;
cats.Add(cat1);
var cat2 = new Cat();
cat2.Name = "Whiskers";
cat2.Age = 2;
cats.Add(cat2);

没有什么可看的。唯一的问题是手工部分-在我的情况下,复杂类型并不像上面的示例那么琐碎。

Nothing fancy there. The only problem is the "by hand" part -- the complex type in my case is not nearly as trivial as the above example.

我还可以查看对象,在调试器中使用任何内置的调试器可视化工具。所以我想我要编写一个自定义的Debugger Visualizer,它将为我生成对象初始化代码。要使用它,我将在调试器中重现该问题,拉出QuickWatch窗口并选择我的自定义可视化工具。

I can also view the object, while in the debugger, with any of the built-in debugger visualizers. So I figured I would write a custom Debugger Visualizer that will generate the object initialization code for me. To use it, I would reproduce the issue in the debugger, pull up the QuickWatch window and select my custom visualizer.

另一种选择是编写一个自定义序列化实现,该实现将序列化为对象初始化代码块。使用此功能比仅拉起快速监视窗口要困难一点,但这可以工作。

Another option would be to write a custom serialization implementation that would "serialize" to a block of object initialization code. To use this would be a bit harder than just pulling up the QuickWatch window, but this could work.

在我自己解决此问题之前,有人做过这样的事情吗?介意分享代码段吗?还是有人建议其他方法?

Before I tackle this problem myself, has anybody done something like this? Mind sharing a code snippet? Or would anyone suggest another approach?

P.S。就我而言,对象的类型是抽象基类的子类。只是想提一下。

P.S. In my case, the type of the object is a subclass of an abstract base class. Just wanted to mention it.

推荐答案

这些建议都行不通。 阅读第一行

These suggestions aren't going to work. Read the first line:


您可以为
编写一个自定义可视化工具,该对象是任何托管对象类,除了
用于对象或数组。

You can write a custom visualizer for an object of any managed class except for Object or Array.

http://msdn.microsoft.com/zh-cn/library/e2zc529c.aspx

有您的答案。如果我阅读正确,则无法通过可视化工具实现。有点of脚。

There is your answer. If I'm reading correctly it can't be implemented through a visualizer. Sort of lame.

这篇关于调试器可视化器生成对象初始化器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆