T 4 beginners – part 2


no comments

T 4 beginners – part 2

T4, VS 2010, Entity Framework, Custom tool

this is the second post on this series.

in this post we will focus on basics T4 Directives.

you can use the code sample of the previous post here.

the series TOC is available here.


Directive syntax:

any of the directives is using the following syntax:

<#@ DirectiveName [ParameterName = "ParameterValue"] #>

it start with <#@ follow with the directive name,

includes zero or more name value parameters and

end with #>

All parameter values must be surrounded by double quotation marks.

If the value itself contains quotation marks, they must be escaped with the \ character.

For example: <#@ DirectiveName Parameter=" \" quote \" " #>


T4 provide the following Directives:



  1. <#@ output extension=".cs" encoding="UNICODE" #>

the output directive define the encoding and extension of the generated file.



The template directive allow the following parameters (all the parameters are optional):

  1. <#@ template debug="true" language="C#" #>

  • language: the language define the programming language of the Control blocks within the T4
    (this is the language that used to process the T4 and it has nothing to do with the T4 output).
    example: language ="C#" or language="VB"
  • debug: indicate whether the template can be debug
    (we will cover debugging on later post)
  • inherits: is one of the possible ways to extend  T4
    (we will discuss the inherit parameter in further post).



The assembly directive define the T4 dependencies.

for example the following T4 Control block:

  1. <#= System.DateTime.Now.ToString() #>

will result with exception unless the following assembly directive will be define:

  1. <#@ Assembly Name="System.Core.dll" #>


The assembly name should be one of the following:

  • The file name of the assembly, if it is located in the same directory as the text template

  • The absolute path of the assembly

  • The relative path of the assembly (with respect to the directory in which the text template is located)

  • The strong name of the assembly, as in the GAC.

  • You can use the $(variableName) syntax to reference Visual Studio or MSBuild variables, and %VariableName% to reference environment variables.



the import directive behave as using statement for the T4 control block.

as the case with other directive it add nothing to the T4 output.

using the following import directive:

  1. <#@ import namespace="System" #>

you can replace the following full path control block:

  1. <#= System.DateTime.Now.ToString() #>

with the following control block:

  1. <#= DateTime.Now.ToString() #>



the T4 directive is fairly intuitive and simple to use.






kick it on DotNetKicks.com Shout it

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

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=""> <s> <strike> <strong>