T4 syntax Quick Start and t4 Quick Start

Source: Internet
Author: User

T4 syntax Quick Start and t4 Quick Start
1. What is T4?

T4, a combination of four English letters starting with T: Text Template Transformation Toolkit. T4 (Text Template Transformation Toolkit) is a code generation engine officially used by Microsoft in Visual Studio 2008. Simply put, you can generate the files you want based on the template, such as class files, text files, and HTML files.

VS itself only provides a code generation execution environment based on the T4 engine, which consists of the following Assembly:

Microsoft. VisualStudio. TextTemplating.10.0.dll

Microsoft. VisualStudio. TextTemplating. Interfaces.10.0.dll

Microsoft. VisualStudio. TextTemplating. Modeling.10.0.dll

Microsoft. VisualStudio. TextTemplating. VSHost.10.0.dll

2. Install the vs plug-in

T4 editing tool http://t4-editor.tangible-engineering.com/Download_T4Editor_Plus_ModelingTools.html

The default VS editing tool is not highlighted or prompted, making it difficult to locate errors. Without this tool, I really don't want to write any T4 code.

3. Early T4 experience

First, create a new class library. Add a new project and select a text file with the suffix xx. tt.

<# @ Template debug = "false" hostspecific = "false" language = "C #" #>< # @ output extension = ". cs "#> // ---------------------------------------------------------------------------------- // <blog garden-Feng Yiyi http://www.cnblogs.com/fenglingyi/ > // This code is automatically generated by the T4 template. // The generation time <# = DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") #> by Feng Yi // changes to this file may lead to incorrect behavior, and if the code is re-generated, these changes will be lost. // QQ: 549387177 // <blog garden-Feng Xiaoyi http://www.cnblogs.com/fenglingyi/ > // Initialize using System; public class UserEntity {public string F_Id {get; set;} public string F_Account {get; set;} public string F_RealName {get; set ;} public string F_NickName {get; set;} public string F_HeadIcon {get; set;} public bool? F_Gender {get; set;} public DateTime? F_Birthday {get; set;} public string F_MobilePhone {get; set;} public string F_Email {get; set;} public string F_WeChat {get; set;} public string F_ManagerId {get; set;} public int? F_SecurityLevel {get; set;} public string F_Signature {get; set;} public string F_OrganizeId {get; set;} public string f_1_mentid {get; set;} public string F_RoleId {get; set;} public string F_DutyId {get; set;} public bool? F_IsAdministrator {get; set;} public int? F_SortCode {get; set;} public bool? F_DeleteMark {get; set;} public bool? F_EnabledMark {get; set;} public string F_Description {get; set;} public DateTime? F_CreatorTime {get; set;} public string F_CreatorUserId {get; set;} public DateTime? F_LastModifyTime {get; set;} public string F_LastModifyUserId {get; set;} public DateTime? F_DeleteTime {get; set;} public string F_DeleteUserId {get; set ;}}View Code
// -------------------------------------------------------------------------------- // <Blog garden-Feng Yiyi http://www.cnblogs.com/fenglingyi/ > // This code is automatically generated by the T4 template. // The generated time is 07:09:29 by Feng Xiaoyi // changes to this file may lead to incorrect behavior. If the code is re-generated, these changes will be lost. // QQ: 549387177 // <blog garden-Feng Xiaoyi http://www.cnblogs.com/fenglingyi/ > // Initialize using System; public class UserEntity {public string F_Id {get; set;} public string F_Account {get; set;} public string F_RealName {get; set ;} public string F_NickName {get; set;} public string F_HeadIcon {get; set;} public bool? F_Gender {get; set;} public DateTime? F_Birthday {get; set;} public string F_MobilePhone {get; set;} public string F_Email {get; set;} public string F_WeChat {get; set;} public string F_ManagerId {get; set;} public int? F_SecurityLevel {get; set;} public string F_Signature {get; set;} public string F_OrganizeId {get; set;} public string f_1_mentid {get; set;} public string F_RoleId {get; set;} public string F_DutyId {get; set;} public bool? F_IsAdministrator {get; set;} public int? F_SortCode {get; set;} public bool? F_DeleteMark {get; set;} public bool? F_EnabledMark {get; set;} public string F_Description {get; set;} public DateTime? F_CreatorTime {get; set;} public string F_CreatorUserId {get; set;} public DateTime? F_LastModifyTime {get; set;} public string F_LastModifyUserId {get; set;} public DateTime? F_DeleteTime {get; set;} public string F_DeleteUserId {get; set ;}}

This is the code class file generated by the preceding T4 template. What T4 can do is far more than this. Here we first understand the basic syntax of T4.

4. T4 syntax

A careful friend just saw some strange syntaxes in the template. The strange thing is that T4 syntax is similar to c # syntax, but it is different from c # syntax.

T4 syntax mainly includes three types: 1 command 2 Text block 3 command Block

<# Standard control block #> can contain statements. <# = Expression control block #> can contain expressions. <# + Class feature control block #> it can contain methods, fields, and attributes, just like an internal class

Commands include template, output, assembly, import, include, and other types.

Here, the command T4 syntax starts with <#

<# @ Command attribute = "value" #>

First Instruction Set

4.1 template instructions

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

4.1.1 langeuage: Output language. Valid values: C # and VB. Default Value: C #

4.1.2 debug: whether to enable debugging. The valid values are true and false. The default value is false.

4.1.3 hostspecific: Valid values: true and false; default value: false. If the value of this feature is set to true, the property named Host is added to the class generated by the Text Template. This attribute is a reference to the host of the conversion engine and is declared as Microsoft. VisualStudio. TextTemplating. ITextTemplatingEngineHost.

4.1.4 inherits: specifies that the program code of the template can inherit from another class, which can also be generated from the Text Template. Currently, wood has been used and can be ignored.

 

4.2 output command

<#@ output extension=".cs" #>

4.2.1 tell the T4 engine to generate a file whose suffix is. cs;

 

4.3 assembly instructions

<#@ assembly name="System"#>

4.3.1 tell the T4 engine to reference the System assembly during compilation and Runtime

$ (SolutionDir): solution directory of the current project
$ (ProjectDir): directory of the current project
$ (TargetPath): absolute path of the current project compilation output file
$ (TargetDir): The current project compilation output directory, that is, the Bin directory of the web project, the debug or release directory under the bin directory of the console and Class Library Project (depending on the current compilation Mode)

For example, we have created a console project MyTest in the root directory of the D disk. The solution directory is D: \ Feng, and the project directory is
D: \ Feng \ MyTest. In Debug compiling mode
The value of $ (SolutionDir) is D: \ Feng.
$ (ProjectDir) value: D: \ Feng \ MyTest
$ (TargetPath) value: D: \ Feng \ MyTest \ bin \ Debug \ MyTest.exe
$ (TargetDir) value: D: \ Feng \ MyTest \ bin \ Debug \

 

4.4 import command

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

4.4.1 tell the T4 engine to reference a namespace during compilation and running. In the code block of the Visual Studio T4 Text Template, the import command allows you to reference elements in another namespace without providing a fully qualified name. It is equivalent to using in C # or imports in Visual Basic. By default, the System namespace reference has been imported.

 

4.5 include commands

<#@ include file="Base.ttinclude"#>

4.5.1 reference a file during runtime, similar to JS reference.

4.5.2 containing commands can improve code reuse rate. For example, we can put some common assembly and namespace references into a file, so we only need to reference them when using them, saves the trouble of re-referencing each time, such as setting up a Reference. the ttinclude file contains common assembly references.

<#@ assembly name="System.Core.dll" #><#@ assembly name="System.Data.dll" #><#@ assembly name="System.Data.DataSetExtensions.dll" #><#@ assembly name="System.Xml.dll" #><#@ import namespace="System" #><#@ import namespace="System.Xml" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Data" #><#@ import namespace="System.Data.SqlClient" #><#@ import namespace="System.Collections.Generic" #><#@ import namespace="System.IO" #>

You only need to use the include command for reference.

<#@ include file="$(ProjectDir)Reference.ttinclude"  #>

4.6 parameter commands

<#@ parameter type="string" name="ParameterName" #>

As the name implies, a parameter is defined to be used elsewhere.

 

Text block 2

Text blocks insert text directly to the output file. Text blocks do not have a special format, just like the classes we wrote in the first experience.

 

Third instruction Block

It is mainly used to control the output of text. You can write arbitrary C # code in the control block.

Standard Control Block:

<#    for(int i = 0; i < 4; i++)    {#>Hello World!<#    } #>

Expression control block:

<#= 1 + 1 #> 

Class control block:

<#+    public class config    {        public static readonly string ConnectionString = "Data Source=(local);Initial Catalog=NFineBase;User ID=sa;Password=hjf19870810;";        public static readonly string DbDatabase = "NFineBase";        public static readonly string TableName = "Sys_Test";    }#>

For example, if you write a class here, you can use this class in other places.

 

In fact, the most detailed learning of the "T4 template" is MSDN. the following link is provided for you to learn more.
  • T4 Text Template Writing Guidelines
  • Use T4 text templates to generate design time codes
  • Walkthrough: Use a Text Template to generate code
  • Code generation during generation
  • Text Template Security
  • Use TextTransform utility to generate a file
  • Run-time text generation using the T4 Text Template
  • Write a T4 Text Template
  • T4 Text Template instruction
  • T4 template commands
  • T4 parameter command
  • T4 output command
  • T4 Assembly commands
  • T4 import command
  • T4 containing commands
  • T4 CleanUpBehavior command
  • Text Template Control Block
  • Text Template utility method
  • Access Visual Studio or other hosts from text templates
  • Use escape sequence in Text Template
  • How to: Use escape sequences to generate templates from templates
  • Debug the T4 Text Template
  • T4 Text Template Writing Guidelines
  • How to: Use a Text Template...
  • Custom T4 text Conversion
  • Text Template conversion process
  • Create a custom T4 Text Template command processor
  • Deploy a custom instruction processor
  • Walkthrough: create a custom instruction processor
  • Call text conversion in VS Extension
  • Use a custom host to process text templates
  • Walkthrough: create a custom text template Host
  • T4 Text Template API reference
  • Namespace
  • Microsoft. VisualStudio. TextTemplating
  • Microsoft. VisualStudio. TextTemplating. Modeling
  • Microsoft. VisualStudio. TextTemplating. VSHost

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.