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

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()); }
#>

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

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,保存,在错误输出窗口显示错误

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

以前就是设计时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类

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

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

T4模板TextTemplatingFileGenerator和TextTemplatingFilePreprocessor区别的更多相关文章

  1. T4模板编辑器

    一 二.工具  (T4模板编辑器) 使用效果 1.tangibleT4EditorPlusModellingToolsVS2013.msi 2.devart T4 Editor for Visual ...

  2. T4模板

    T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit. T4文本模板,即一种自定义规则的代码生成器.根据业务模型可生成任何形式的文本文件或供程序调 ...

  3. 你必须懂的 T4 模板:深入浅出

    示例代码:示例代码__你必须懂的T4模板:浅入深出.rar (一)什么是T4模板? T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit. T4文 ...

  4. 三、T4模板与实体生成

         上文我们最后虽然用模板创建了一个实体类,但是类的内容仍旧是静态的,这里我们需要用动态方式生成类的内容.因为需要查询数据库这里又免不了各种繁琐的连接数据库操作,为了使我们的编码更加直观,仍然采 ...

  5. T4模板使用技巧

    =============C#.Net 篇目录============== 示例代码:示例代码__你必须懂的T4模板:浅入深出.rar (一)什么是T4模板? T4,即4个T开头的英文字母组合:Tex ...

  6. 使用T4模板动态生成邮件内容并储存到任意位置

    一.基础概念介绍 T4模板是扩展名为 .tt 的文本文件. 他分为设计时模板 和运行时模板.主要区别在于在vs中右键点击文件,打开“属性”,在“自定义工具”一栏中的值分别如下: 设计时模板: Text ...

  7. Asp.net MVC企业级开发(09)---T4模板

    T4即为Text Template Transformation Toolkit,一种可以由自己去自定义规则的代码生成器.根据业务模型可生成任何形式的文本文件或供程序调用的字符串 在VS中T4模板是没 ...

  8. T4模板生成文件要点记录

    可以使用 $(variableName) 语法引用 Visual Studio 或 MSBuild 变量(如 $(SolutionDir)),以及使用 %VariableName% 来引用环境变量.介 ...

  9. JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(四):自定义T4模板快速生成页面

    前言:上篇介绍了下ko增删改查的封装,确实节省了大量的js代码.博主是一个喜欢偷懒的人,总觉得这些基础的增删改查效果能不能通过一个什么工具直接生成页面效果,啥代码都不用写了,那该多爽.于是研究了下T4 ...

随机推荐

  1. Spring Framework------>Class RestTemplate----->

    org.springframework.web.client.RestTemplate 官方文档 学习心得: class RESTTemplate用于管理与客户端的HTTP连接

  2. RHEL 集群(RHCS)配置小记 -- 文档记录

    1.RHEL 6 集群配置官方管理手册 https://access.redhat.com/site/documentation/zh-CN/Red_Hat_Enterprise_Linux/6/pd ...

  3. IT公司笔试题(一)

    1.  已知一个递归算法的算法复杂度计算公式为T(n) = T(n/2) + n,则T(n)的算法复杂度为多少? 解:O(n) T(n) = T(n/2) + n = T(n/4) + n/2 + n ...

  4. android里R.layout.的问题

    今天,在Exlipse里的一个项目在.java文件里写  setContentView(R.layout.activity_problem);时,显示错误,以为是R.java文件里没有对应的activ ...

  5. zabbix监控超详细搭建过程

    监控及zabbix 目录: 1       监控分类... 1 1.1        硬件监控... 1 1.2        系统监控... 2 1.3        网络监控... 3 1.4   ...

  6. 0728pm 控制器

  7. Ceph分层存储分析

    最近弄Ceph集群考虑要不要加入分层存储 因此花了点时间研究了下 1,首先肯定要弄清Ceph分层存储的结构 ,结构图大概就是下图所示 缓存层(A cache tier)为Ceph客户端提供更好的I/O ...

  8. JQuery教程

    1.是javaScript库(js文件) 2.使用:script标签 3.语法:$开头 $().action() 列如:$('div').css("color",'red'); 4 ...

  9. hdu 4268 Alice and Bob

    Alice and Bob Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  10. 【洛谷P1541】乌龟棋

    四维dp #include<cstdio> #include<cstring> using namespace std; ; ],a,b,c,d,n,m; int max(in ...