T4 Basis

T4 is a language tags that can be used in Visual Studio to generate file(s) (one file by default).

T4 only has 4 tags:

  • <#@ … #>
  • <# … #>
  • <#= … #>
  • <#+ … #>

Every thing that is not in tags is written as is on the generated file.

 

<#@ … #>

We use this tag in order to define information your meta-code depends on:

<#@ template language="C#" hostspecific="true"#>

language: With T4, you can write your meta-code with C# and VB.NET. So language is useful to specify the language you use.

hostspecific: this is useful to use relative path from the T4 location.

<#@ output extension="…"#>

You have to use it to define default generated file extension (that will have the same name than the T4 file)

<#@ assembly name="…" #>

Use it to add references that you want to use in your meta-code

<#@ import namespace="…"#>

Use it to add usings to your code (for avoiding typing namespaces or to use alias)

<#@ include file="…"#>

This is very used by WAQS. The idea of the include file is to reference another T4 file. So you can share some meta-code between many T4.

When you use include, the T4 engine merge all files together. However, this merge is “stupid”.

Ex:

image

This T4 doesn’t work because T0.ttinclude meta-code is duplicated twice.

So, even if only T1.ttinclude and T2.ttinclude use T0.ttinclude, you have to use this kind of include:

image

This is not cool, particularly when you use an editor with intellisense because when you edit T1.ttinclude file, the editor has no way to know that it has to use T0.ttinclude.

However, sometimes, this is could be useful.

For example, in WAQS, sometimes you have different code generation if you use .NET 4.5 or .NET 4.0 or if you use the PCL client or the WPF one. So in this case, the based .ttinclude can call a method (without knowing it) that is defined in a specific ttinclude file that is referenced by the main T4 file as the based one.

 

<# … #>

This tag contains the meta-code that drives code generation.

If we compare it with a console application, it is the Main method.

 

<#= … #>

This code contains an expression that is evaluated by T4 engine in order to write the result in the generated file.

 

<#+ … #>

This tag allows to define API usable in your meta-code.

You can define anything here such as methods, classes, properties, etc.

However, because T4 engine merge all in one class, you can’t define static classes (so you can’t define extension method for example).

 

 

Sample

image

The above meta-code generates this file:

image

 

Or you can do the same with a .ttinclude that contains all the meta-code as WAQS does:

image

See also: How does WAQS code generation work?

This entry was posted in 10511, 17895. Bookmark the permalink.

One Response to T4 Basis

  1. Maher Jendoubi says:

    Nice post to get started with T4 which stands for Text Template Transformation Toolkit! It is also an important basic step to understand how WAQS generate code based on specification file. Are they any limitations with Visual Studio 2013 when we are using T4 for code generation?

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>