开发者

Debugger Visualizer to generate Object Initializer code

开发者 https://www.devze.com 2023-01-25 20:32 出处:网络
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 r

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 bloc开发者_运维技巧k, by hand, such as the following one:

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

or even something like this:

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.

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. In my case, the type of the object is a subclass of an abstract base class. Just wanted to mention it.


Here is a tool called Object Exporter which does exactly what you are looking for, it will generate C# initialization code from any object in your debugging windows:

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

Debugger Visualizer to generate Object Initializer code

Blog Post with more information:

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


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/en-us/library/e2zc529c.aspx

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


I don't know of any existing feature / functionality which provides the behavior you are looking for. But the idea of writing a Visualizer to generate the code for a given object seems sound for simple object types: those consisting of properties of primitive types. Once you get beyond that the task grows increasingly difficult.

In the visualizer you'll have access to the object in question and can use tricks like reflection to get at the members. Reperesenting a member's value as code is easy for primitive types: int, string, etc .... This is much harder for non-primitive types because the process becomes recursive and you need to consider object cycles.

If this does work for you here's a quick tutorial on Debugger Visualizers

  • http://blogs.msdn.com/b/lior/archive/2009/05/31/creating-a-debugger-visualizer-for-visual-studio-c-tutorial.aspx

All in all it may be easier to just hand write up each scenario :(

0

精彩评论

暂无评论...
验证码 换一张
取 消