Let's take a glimpse of the next T4, here's how to start introducing T4 templates. About T4 template Introduction and use of online search a basket, want to go deep into the study can find some of the information, here only to introduce the next we need to use some knowledge, not all the knowledge of T4 template. T4 templates use a similar approach to ASPX files, so it doesn't take much time.
Open the TT file, enter the following code and Save:
@ assembly indicates the target assembly used by the TT template environment
@ Import indicates the namespace used by the TT template environment
@ output indicates the TT template output format, including the file suffix encoding method, etc.
The code contained in <##> is the control block
Text blocks that are not contained in <##> are generally exported as static text.
Example, create a 1x5 table and output it in HTML format:
<#@ Template debug="false"Hostspecific="false"Language="C #"#><#@ Assembly Name="System.core"#><#@ Importnamespace="System.Linq"#><#@ Importnamespace="System.Text"#><#@ Importnamespace="System.Collections.Generic"#><#@ Output extension=". html"#><table> <tr><# for(intI=0;i<5; i++){#> <td></td><#}#> </tr></table>
Just change the <#@ output extension= ". html" #> to. html. From here you can see that the basic and the use of ASPX are the same. In addition to the individual instructions need to view the following information, Basic C # can be used directly.
Having said so much, the role of the T4 template in our project development seems to have not been reflected. Here is an example to illustrate the application of the T4 template.
Believe that the vast majority of people are tired of the realism of the class, no one would be foolish to manually write the entity class, unless it is necessary business entities. How to automatically generate corresponding entities based on database tables is a simple requirement for us. Here's how to do this with the T4 template (of course vs itself has this function, we just restore the implementation of this feature):
Create a new Class library project Entitygenerator, creating an Entity template file Entitytemplate.tt, enter the following code and Save:
<#@ Template debug="false"Hostspecific="false"Language="C #"#><#@ Assembly Name="System.core"#><#@ Importnamespace="System.Linq"#><#@ Importnamespace="System.Text"#><#@ Importnamespace="System.Collections.Generic"#><#@ Output extension=". CS"#>usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceentitygenerator{ Public classtestentity {/// <summary> ///numbering/// </summary> Public stringId {Get;Set; } }}
Open the generated EntityTemplate.cs file to see the following:
Yes, a complete class file has been generated, and the next step is to make the properties of the class dynamically generated by connecting to the database. This will be demonstrated in detail in the next article. Because so far there is not too much code, to the post-code amount, I will be uploaded together with the project.
Second, T4 template