t4 templates Quick Start

grammar

Including instructions, text blocks, the control block.

1. Instruction

Instruction includes template, output, assembly, import, include other types, how to tell the T4 engine to compile and run a template. These instructions corresponding to T4 engine configuration parameters.
Example:

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

Tell T4 engine control block written in c #;

  • langeuage: output language, the effective value of C #, VB, C # default
  • debug: Whether to enable debugging, the effective value of true, false, default is false.
  • hostspecific: Valid values ​​true, false, default is false. If this property is set to true value, it will be called the Host attribute to the text generated by a template class. The property is a reference to the host transformation engine and declared Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost.
  • inherits: program code can be specified template can inherit from another class, which can also be generated from a text template. Currently there are wood used, basically ignored
<#@ output extension=".cs" #>

Tell T4 engine generates a file name suffix .cs;

<#@ assembly name="System.Core"#>

Tell T4 engine assembly references System.Core compile and run time;

<#@ assembly name="$(SolutionDir)\Project.CodeGenerator\bin\Debug\MySql.Data.Dll"  #>

Tell T4 engine reference assemblies on a specific location;

$ (SolutionDir): Current Project Solutions Catalog
$ (ProjectDir): The current project directory
$ (TargetPath): The current project compiler output file absolute path
$ (TargetDir): The current project build output directory, web Bin directory of the project, console, under the Class library project bin directory debug or release directory (depending on the current compilation mode)

For example: For example, we built on the D root directory of a console project MyTest, solutions directory to D: \ Feng, the project directory is
D: \ Feng \ MyTest, then the time to compile in Debug mode
$ (SolutionDir) the value of D: \ Feng
$ (ProjectDir) value of D: \ Feng \ MyTest
$ (TargetPath) is D: \ Feng \ MyTest \ bin \ Debug \ Mytest.exe
$ (TargetDir) is D: \ Feng \ MyTest \ bin \ Debug

<#@ import namespace="System.Data.SqlClient"#>

A reference to a namespace tell T4 engine run-time compiler

<#@ include file="../Code/DBSchema.ttinclude"#>

A reference to a document compiled runtime told T4 engine, similar to the JS reference

1.2 text block

Text blocks, the content of the text block T4 engine will directly copied to the output file.

1.3 Control Block

A control block, for outputting a control main text. In the control block may write arbitrary C # code.

<# 标准控制块 #> 可以包含语句。

<#= 表达式控制块 #> 可以包含表达式。

<#+ 类特征控制块 #> 可以包含方法、字段和属性,就像一个类的内部

1.4 Hello world example

#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".txt" #> Hello, <#Write("World");#>

Guess you like

Origin www.cnblogs.com/tangge/p/11403048.html
Recommended