I found myself today needing some simple DTO classes for unit tests to extend existing services and controllers. We’re using .NET 3.5 so Object and Collection Initializers come in really handy for mocking out services and providing data. If the object graph is somewhat bigger crafting the data by hand is quick but somewhat cumbersome.
Yet we usually have that data somewhere and getting a hand on an instance that might just need a bit of tweaking later is easy. I really wanted something that would serialise an instance of an object (perhaps obtained while debugging) into object initialiser syntax that I can paste back into the code.
I was disappointed I couldn’t google anything useful, but then perhaps my googling skills are not as good as my coding skills so I wrote something quickly instead. A bit of reflection and recursion and it’s there.
You can download it from CodePlex http://objectserialiser.codeplex.com/
It is extremely sketchy but does the job for me.
Is anything better out there? Ideally, this could now be hooked into Visual Studio somehow to make the job even easier. Unfortunately Code Visualisers won’t do the job as they can’t be registered for “object”.