T4 Samples

Hi, my name is Mårten; I am a part of the T4 team. A couple of years ago, a friend of mine introduced me to T4. That event has transformed my way of thinking about programming more than anything else. Using T4, which is a code-generation tool, I have improved my productivity and the quality of my work: In short, I have found the answer.

I am therefore very enthusiastic to spread the good word: The word that code-generation (using T4) is an excellent addition to your standard programming toolbox.

I realize that getting started with T4 can seem unusual and awkward at first, which is why I’ve prepared some T4 samples.

The samples

My T4 samples will focus on bottom-up design-time templates. What I mean by this is that the focus is on simple templates that get executed at design-time, and that mitigate small but irritating problems, such as Exception classes in C#. T4 can also be used to address top-down problems and for templates that are executed at run-time.

The samples are shipped as a Visual Studio 2010 solution that can be downloaded from http://t4talk.codeplex.com as a Mercurial repository or as a zip-file.


The first sample “HelloWorld” is located under the project folder “1_HelloWorld”, similarly the second sample is located under the project folder “2_Exceptions”. T4 template files have the extension “.tt”. Note: At the present stage Visual Studio 2010 doesn’t have IntelliSense for T4 templates, but don’t let this discourage you; T4 is worth it.

Almost all of the documentation is located directly in the template source as I would like to keep the documentation and the sample code strongly coupled.

In order to find the start of the documentation in the each sample you can search for the phrase:
// +++ Documentation

One very important pattern when developing T4 templates is separating the “Model” from the “Template”. The Model is declarative, the template is executed on the model to produce the output; it’s a bit like XML/XSL.

In order to find the model of the each sample you can search for the phrase:
// +++ Model

In order to find the template of the each sample you can search for the phrase:
// +++ Template

I hope that these samples will help your team to get started with T4 and code-generation.

Best regards,


Comments (4)

  1. T4 Rocks says:

    T4 is a great feature not promoted enough by MS!

  2. Andreas Grech says:

    Great stuff for people getting into T4 and code generation.  Kudos to you!

  3. DNAfor.NET says:

    i don't see anything to download on the  codeplex site?

  4. DNAfor.NET says:

    oh i think i see it.. got to go to source, not downloads

Skip to main content