madhavrao… » T4 - Built-In Directives

T4 - Built-In Directives

The five built-in directives are:

  1. Assembly
  2. Import
  3. Include
  4. Template
  5. OutputĀ 

Assembly

The assembly directive identifies an assembly to be referenced so that you can use types within that assembly from code in the text template. Using the assembly directive is equivalent to using the Add Reference feature in Visual Studio.

In the following example, the directive name is assembly, the parameter name is name, and the parameter value is SomeLibrary.DLL:

<@ assembly name=”SomeLibrary.DLL” #>

Import

By using the import directive, you can refer to types in a text template without providing a fully qualified name.

In the following example, the directive name is import, the parameter name is namespace, and the parameter value is System.Diagnostics:

<#@ import namespace=”System.Diagnostics” #>

Include

The include directive processes text from the specified file as if that text were included verbatim in the template currently being processed. In the following example, the name of the directive is include, the parameter name is file, and the parameter value is C:\test.txt:

<#@ include file=”c:\test.txt” #>

Template

By using the template directive, you can specify characteristics of the generated transformation class, such as code language, class inheritance, culture, and the ability to debug. The template directive takes the following parameters, each of which is optional:

Parameter Default Value Acceptable Value Details
language C# C#, VB This parameter specifies which language, either Visual Basic or C#, has been used for code that is inside statement and expression blocks. All code that the engine generates in the transformation class must be expressed by using this language. The following example shows how to specify the Visual Basic language: <#@ template language=”VB”#>
inherits TextTransformation Any Class that derives from TextTransformation This parameter specifies which class should be used as the base class for the generated transformation class. The following example shows how to specify that the SomeClass class should be used:<#@ template inherits=”SomeClass”#>This concept is similar to ASP.Net Pages. Where markup is defined in aspx file and code behind is in .cs or .vb file or in some dll.
culture “”, the invariant culture A culture expressed as a string in the form xx-XX. For example, en-US, ja-JP, de-CH, de-DE, and others. This parameter specifies the culture to use when an expression block is converted to text.
debug true true, false This parameter specifies whether debugging is enabled. The following example shows how to enable debugging:
<#@ template debug=”true” #>
hostspecific false true, false This parameter is used only with custom hosts. If you set the value of this parameter to true, you can access a property called Host in your text template. The property is a reference to the object that hosts the engine. You should set hostspecific to true only if you want to write a text template that is specific to a custom host and the text template calls the host at execution time. When hostspecific is true and you are using Visual Studio, you can call GetService to update your text templates

Output

By using the output directive, you can specify characteristics of the generated text output, such as the file name extension. The output directive takes the following parameters, each of which is optional:

Parameter Default Value Acceptable Value Details
extension .cs Any string that satisfies the file systems rule for file extension. This parameter specifies the extension of the generated text output file. For example, you can specify a .cs or .vb extension if the generated text output is a code file. It is important to remember that the file name extension indicates only that the file is intended to be a particular format. The writer of the text template must make sure that the generated text meets the requirements of the intended format.<#@ output extension=”.txt” #>
<#@ output extension=”.htm” #>
<#@ output extension=”.cs” #>
<#@ output extension=”.vb” #>
encoding “Default”, the current ANSI code page of the system. Any of the following members of the Encoding class expressed as a string: Default, ASCII, BigEndianUnicode, Unicode, UTF32, UTF7, UTF8. This parameter specifies what encoding should be used when the generated text output file is created.<#@ output encoding=”ASCII”#>
<#@ output encoding=”UNICODE”#>
<#@ output encoding=”UTF8″#>

Share this post :

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>