Text Template Transformation Toolkit

The TextTemplateTransformationToolkit, or T4, used to be a part of the DomainSpecificLanguage add-on for VisualStudio, but it has been mainstreamed.

It was part of Visual Studio 2008 (even without downloading any add-ons), but it was hidden; to create a text template you would add a file to your project with the .tt extension.

In Visual Studio 2010, you can create two different kinds of text templates from the File | New menu.

A text template is sort of like PHP, except that the code parts are written in VB or C#. You can use the code to make portions of the text conditional or repeating. You can also insert the results of computation into the text.

One type of text template inserts the generated file directly into your project. So you can, for example, generate C# code, which can then be compiled as part of your project along with everything else. However, text templates can generate any (text based) type of file; for example, they can generate plain text, or comma-delimited files, or HTML, or XML.

The other type of text template generates a class which will then generate the output at runtime. So if you have a program that generates HTML output, you can use a text template to describe the output, and then use the generated class to actually generate it, based on the template.

The code in a text template can also read from files, databases, or other sources.


The TextTemplateTransformationToolkit and ReflectionEmit are both used for CodeGeneration. T4 is used at compile-time whereas ReflectionEmit can be used at runtime.


EditText of this page (last edited November 27, 2011) or FindPage with title or text search