首页 技术 正文
技术 2022年11月15日
0 收藏 592 点赞 2,958 浏览 2754 个字

最近做一个项目,需要根据数据库表生成对应的实体类,于是想到了代码生成器。查阅了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上编写文本或者代码。

 <#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="$(TargetDir)\Client.exe" #>
<#@ import namespace="Client.Templates" #>
<#@ output extension=".txt" #>
<#
for(int i = ; i < ; i++)
{ WriteSquareLine(i);
#>
xx a <#=i#>
<#
}
#>
<#
Util1.HelloWorld();
#>
<#
var bb=Util1.Method2();
#>
<#=bb#>
End of list.
<#+ // Class feature block
private void WriteSquareLine(int i)
{ Console.WriteLine(i.ToString()); }
#>

然后保存,在模板相同的文件夹下就输出模板生成文件

T4模板TextTemplatingFileGenerator和TextTemplatingFilePreprocessor区别

3. 我们在c# 类Util1 添加一个方法Method3

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Client.Templates
{
public class Util1
{
public static void HelloWorld()
{
Console.WriteLine("ni haoqqqqxxx!");
} public static string Method2()
{
return "Method2 from c# code";
} public static string Method3()
{
return "code from c# class Util1 method Method3 ";
} }
}

4、在HelloWorld.tt  调用Util1.Method3,保存,在错误输出窗口显示错误

T4模板TextTemplatingFileGenerator和TextTemplatingFilePreprocessor区别

为什么呢 ? 在Util1.Method3() 确定已经存在,怎么提示不存在呢。 原来这就是TextTemplatingFileGenerator类型模板的关键。 使用TextTemplatingFileGenerator编写设计时(design-time)模板,需要在调用前,编译后,再在模板上调用。刚才我们编写Util1.Method3()后,但还没有编译就调用了。所以报错。现在我们删除模板调用的代码<#=Util1.Method3()#>,编译项目,编译成功后,再在模板上调用<#=Util1.Method3()#>,模板就可以保存成功不报错了。

T4模板TextTemplatingFileGenerator和TextTemplatingFilePreprocessor区别

以前就是设计时design-time模板使用TextTemplatingFileGenerator的用法,需要注意的模板调用的类,方法,变量等,需要编译后在调用。

二、使用TextTemplatingFilePreprocessor做运行时run-time模板

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

1、添加模板文件SecondTemp.tt ,将自定义工具(custom tool)改为TextTemplatingFilePreprocessor。然后保存自动生成SecondTemp.cs类

T4模板TextTemplatingFileGenerator和TextTemplatingFilePreprocessor区别

2、在c# 类Util1 添加一个方法Method4,并在SecondTemp.tt模板上调用,点击保存,此时我们在c# 类Util1 添加一个方法Method4后并没有编译项目,保存时时没有保存,但也不生成输出

文件SecondTemp.txt。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Client.Templates
{
public class Util1
{
public static void HelloWorld()
{
Console.WriteLine("ni haoqqqqxxx!");
} public static string Method2()
{
return "Method2 from c# code";
} public static string Method3()
{
return "code from c# class Util1 method Method3 ";
} public static string Method4()
{
return "code from c# class Util1 method Method4";
}
}
}

3、在程序里调用模板SecondTemp.tt,并生成输出文件SecondTemp.txt

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Client.Templates; namespace Client
{
class Program
{
static void Main(string[] args)
{
SecondTemp temp = new SecondTemp();
String pageContent = temp.TransformText();
System.IO.File.WriteAllText("SecondTemp.txt", pageContent);
Console.WriteLine("task done!");
Console.ReadKey();
}
}
}

4、在目录下就可以看到SecondTemp.txt

T4模板TextTemplatingFileGenerator和TextTemplatingFilePreprocessor区别

以上就是TextTemplatingFileGenerator和TextTemplatingFilePreprocessor在使用上的区别。谢谢你的浏览

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295