Saturday, 25 August 2012

Project Template Locations in VS2012

Adding a Project Template 

If you've seen my post on creating a console application in VS Express 2012 for Web, you'll be thinking "why doesn't he create a template?" Well I did, via the Export Template Wizard in visual studio (File -> Export Template...)

This wizard creates a zip file of all the items in the current project. When this is copied to the correct location on your machine, it can be used to create a new project with all your previous goodness. The location to copy it to is [Visual Studio 2012 folder]\Templates\ProjectTemplates\Visual C# or[Visual Studio 2012 folder]\Templates\ProjectTemplates\Visual Basic

This allows you to select your template in the New Project dialog box

Specifying the Project Type

If you want the template to appear under one of the project types, e.g. Windows, put the .zip file into a directory of that name, e.g. [Visual Studio 2012 folder]\Templates\ProjectTemplates\Visual C#\Windows

Can I Have it in Both?

If you want it in both locations, Visual C# and Windows, you need to edit your template. Open the .zip file and there'll be a .vstemplate file; this is the definition of the project template. You need to add an element NumberOfParentCategoriesToRollUp. This tells VS to show the template in the project subtype (Windows) and its parent (Visual C#). Here's an example:

Creating a Console App in Visual Studio Express 2012 for Web

I've installed and am using Visual Studio Express 2012 for Web as my primary IDE for C#. There isn't a template for a console application, so if you want 1980s goodness, create a new class library and on the Properties page, setting Output Type to Console Application, like this

This then allows you to create a class with a Main method

and run the app with a push of F5

**UPDATE** I have created a project template that creates a console application, it's available on github, here with instructions on how to install it.

Sunday, 19 August 2012

Creating a VisualizerObjectSource

I did a quick introduction talk on Visual Studio Debugger Visualisers the other day, and one question I was asked was [something like] "Do you have to serialise the object to be visualised? Could you pass it to the visualiser and have it read the properties via reflection?"

The answer is yes and no. You do have to serialise the object to a stream, but you can implement your own method of serialisation. I'll try to explain that; the data to be visualised must pass between the process boundaries of the running process and the debugger process via a System.IO.Stream. The contents of this stream can be whatever you want, as long as the visualiser knows how to interpret it. Here's an example:

** If you haven't seen debugger visualisers before, check out my previous post here to give some background **

This is the object to be visualised:

Note the second argument to the DebuggerVisualiser attribute - this is vital to ensure that the correct class is used to provide the object to be visualised.

As the object is not marked as serialisable, it can't be provided to the visualiser via the normal GetObject method of the IVisualObjectProvider (the GetObject method relies on serialisation). However, what you can do is create a custom implementation of VisualizerObjectSource, to be used by the objectProvider. VisualiserObjectSource has a virtual method, GetData, with two parameters. The first parameter is an object, which will be the object to be visualised. The second parameter is a stream, which needs to be populated with the visualisation data for the object:

In this example, I'm passing an integer which represents the Argb value of the object's colour.

When the Show method on the visualiser is called, it's passed an instance of IVisualizerObjectProvider. With XML serialisable objects you would normally obtain the object to be visualised via the objectProvider.GetObject method. When the object is not serialisable, you need to call the GetData() method instead; the objectProvider calls the GetData method of the VisualizerObjectSource, and returns the contents of its outgoingStream argument. This can then be used to visualise the object:

When this is all put together, the visualiser looks like this:

The code from this example is on GitHub: