Thursday, February 23, 2012

Creating custom Visual Studio Item Templates

If you are using Visual Studio 2010, you probably know that you can easily create new Item Templates based on existing file by simply selecting File-->Export Template and using the Item Template option in the Wizard.

However, this quick and easy solution of creating Item Templates may leave quite a lot be desired in terms of customizing your Item Templates for future use.  The file names may not follow any standard conventions and things such as namespaces, class names, interfaces etc. may be exported as-is, thus providing limited re-usability.

Instead, what you will probably want to do is customize the content in the file prior to exporting the content as an Item Template.

Fortunately, there is a syntactical language available for providing just this type of functionality.  You can read more about this syntax here: http://msdn.microsoft.com/en-us/magazine/cc188697.aspx

If you want to see some examples of how this syntax is used, you have to look no further than your own Visual Studio 2010 installation!

Beneath the Program Files directory, you should find the following directory structure:
Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\

Below this directory you will find Item Templates for all of the various Item Templates that ship by default with Visual Studio 2010.

Now, if you navigate into any of these directories (such as CSharp), you will find several subdirectories corresponding to the various Item Template categories you find while creating New Items in Visual Studio.

If you go into the Code Directory and the 1033 subdirectory (for the English language), you will find numerous .zip files.  Interestingly enough, all of the Item Templates in Visual Studio are simply represented as nothing more than .zip files!

Since they are just .zip files, you can examine any of them by using any number of Zip Extraction tools (including Windows Own Built-in Zip Utility that ships with Windows Vista and later).

For example, if I open up the Class.zip file, I will find 2 files contained within:
  1. Class.vstemplate
  2. Class.cs
If I now open up the Class.cs file, I will see content such as the following:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}



The $<variable name>$ syntax is the internal substitution syntax that is being used to perform the required namespace and class processing that occurs when you add a new Class file to your project. As you can see, this maps over to the Built-In Replacement parameters provided in the link above.

Finally, if you open the Class.vstemplate file, you will also be able to see how the Template Definition is created so that it may be used in Visual Studio.
 
In future articles, I will begin examining some of the additional nuances and details about creating and using  Visual Studio Item Templates, but for now, you should have enough to get you started towards creating your very own Item Templates!



No comments:

Post a Comment