T 4 beginners – part 2
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.
any of the directives is using the following syntax:
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:
the output directive define the encoding and extension of the generated file.
The template directive allow the following parameters (all the parameters are optional):
- 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:
will result with exception unless the following assembly directive will be define:
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:
you can replace the following full path control block:
with the following control block:
the T4 directive is fairly intuitive and simple to use.