Posted by: rcosic | 01/04/2010

T4 – Code Generation By Using Code Templates

I you’ve followed my posts about CodeDOM and code generation, probably you tought that this process is boring and cumbersome. Well, with T4, the things are definitely improved.

I’ll try to present you new code generator, which is part of Visual Studio, more than a standalone tool, for generating the code. You have on net some pretty good blog posts on that one tool. Check out the following if you are more interested:

First of all, this is not a classical tool, but rather a new way on how the chunks of code could be created out of templates. T4 stands for text templating, that is, making some body of text generated by using of some logic (inside of template file).

Open Visual Studio and add new item – call it whatever you like, but make sure it ends with .tt. This extension stands for text template file, and VS automatically recognize it and sets its Build Action to TextTemplatingFileGenerator.

When you build the project, a resulting file (for example, LiveObject.cs) will be present under this template file (when you expand the file structure).

T4 has a special tags which makes the generating of files practical. For example, you add the references to the file with an import directive, etc. But more important are ‘<#=’  and  ‘<#+’  tags which allows you to add some logic into the text to create a content – similar you do in ASP.NET when you add ‘server tags’ to add server logic to the HTML. Let’s see it in a real example:

<#@ template language=”C#” debug=”true” #>

<#@ output extension=”cs” #>
<#@ import namespace=”System.Collections” #>
<#@ import namespace=”System.Collections.Generic” #>
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.

using System;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;

namespace T4Example.T4Classes
public partial class <#= this.ClassName #> : INotifyPropertyChanged
#region [ Instance fields ]
<# for (int idx = 0; idx <; idx++) { #>
private <#=[idx,0] #> _<#=[idx,1].ToLower() #>;<# }#>


#region [ Properties ]
<# for (int idx = 0; idx <; idx++) { #>
public <#=[idx,0] #> <#=[idx,1] #>
return _<#=[idx,1].ToLower() #>;
_<#=[idx,1].ToLower() #> = value;
<# } #>

#region [ INotifyPropertyChanged Members ]

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));


<#+ string ClassName = “T4Customer”; string[,] properties = { {“string”, “Name”}, {“decimal”, “Turnover”} }; #>


As you can see, there is no coloring of code, and no Intellisense. There are some commercial products that offer such possibilities. Anyway, look at this text and you will find couple of interesting points…

First, there are some directives on what to generate (in our case C# code which can be even debugged!), which namespaces to import, some comments etc. After that, the actual code is just written in plain text. This text content will be placed ‘as-is’ in a generated file. Only those chunks marked with special tags (mentioned before) will be interpreted as ‘logic’ and the resulting text will be also printed in those places.

And finally, here are the prinout of the resulting class. This is actually one POCO object which implements INotifyPropertyChanged (suitable for data binding and serialization):


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: