T4 template basics, t4 Template
Many T4 templates were used to generate code in the educational administration system. So what exactly does the T4 template do? Why should we use the T4 template? Here, we will briefly understand its role.
When creating a T4 template, the following code segment is displayed by default:
<#@ template debug="false" hostspecific="false" language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #><#@ output extension=".txt" #>
The above code is the instruction block in the T4 template, that is
Template instruction: indicates that the C # code template, assembly instruction, import instruction, output instruction, and parameter instruction and include instruction are used here. I did not come into contact with so many details during the application process, but I just understood it briefly. Here I will explain a small example in the system:
<# @ Template language = "C #" debug = "false" hostspecific = "true" #> <# @ include file = "EF. utility. CS. ttinclude "#> <# @ output extension = ". cs "#> // output here is. CS file <# CodeGenerationTools code = new CodeGenerationTools (this); MetadataLoader loader = new MetadataLoader (this); CodeRegion region = new CodeRegion (this, 1 ); // region code MetadataTools ef = new MetadataTools (this); string inputFile = @".. \ ITOO. basic. model \ BasicEntity. edmx "; // here EdmItemCollection ItemCollection = loader. createEdmItemCollection (inputFile); // create an EdmItemCollection and load the metadata string namespaceName = code. vsNamespaceSuggestion (); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager. create (this); #> using ITOO. basic. model; // These are text blocks using ITOO. library. core; namespace ITOO. basic. IBLL {<# foreach (EntityType entity in ItemCollection. getItems <EntityType> (). orderBy (e => e. name) // These are the control blocks {#> public partial interface I <# = entity. name #> BLL: IBaseService <# = entity. name #>>{}<##>}
Text block: directly copy to the output content.
Control Block: the program code that inserts variable values into the text and controls the conditions or repeated parts of the text. control blocks cannot be nested in control blocks. This includes changeable parameters, for example, <# = entity. Name #> expressed by <#... #>.
The above are some simple understandings of T4 templates. In terms of functions, they mainly aim to reduce code duplication. This allows developers to reduce repetitive work. For more details, see http://www.cnblogs.com/heyuquan/archive/2012/07/26/2610959.html