最近做一个项目,需要根据数据库表生成对应的实体类,于是想到了代码生成器。查阅了Nvelocity、T4、RazorEngine,对于一个微软技术派,觉得还是T4最亲切,使用简单,功能强大。 

    在尝试使用T4时,遇到了一些问题,这些问题使我弄明白了TextTemplatingFilePreprocessor和TextTemplatingFileGenerator在使用上的区别。

一、使用TextTemplatingFileGenerator做设计时design-time 模板。

官方csdn上的网址 https://msdn.microsoft.com/en-us/library/vstudio/dd820620.aspx

1、创建模板文件,默认情况下,模板文件的扩展名为.tt。自定义工具(custom tool)为TextTemplatingFileGenerator。

T4模板TextTemplatingFileGenerator和TextTemplatingFilePreprocessor区别

2、我们在HelloWorld.tt上编写文本或者代码。

 1 <#@ template debug="true" hostspecific="true" language="C#" #>
 2 <#@ assembly name="$(TargetDir)\Client.exe" #>
 3 <#@ import namespace="Client.Templates" #>
 4 <#@ output extension=".txt" #>
 5 <#
 6    for(int i = 0; i < 4; i++)
 7    {  WriteSquareLine(i); 
 8    #>
 9   xx a <#=i#>
10   <#
11    }
12 #>
13 <#
14  Util1.HelloWorld();
15 #>
16 <#
17 var bb=Util1.Method2();
18 #>
19 <#=bb#>
20 End of list.
21 <#+   // Class feature block
22 private void WriteSquareLine(int i)
23 {
24 
25   Console.WriteLine(i.ToString());
26 
27 }
28 #>
View Code

相关文章:

  • 2021-07-13
  • 2021-07-09
  • 2021-08-23
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
  • 2022-02-11
  • 2021-12-21
相关资源
相似解决方案