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

    转自http://relistan.com/a-week-with-mozilla-rust/ A Week with Mozilla's Rust I want another systems la ...

  2. 利用Maven建立java web项目

    方法一:在IntelliJ IDEA中创建maven web项目 一.在新建项目对话框中,选择“Maven”类型,设置好jdk,勾选“create from archetype”,在列表中选择“mav ...

  3. Multiple annotations found at this line: - The content of element type "mapper" must match "EMPTY". - Attribute "namespace" must be declared for element type "mapper".

    今天在mybatis的mapper映射配置文件中遇到了这样的问题,困扰了我3个小时: Multiple annotations found at this line: - The content of ...

  4. nvidia 各种卡

    cudnn是针对maxwell优化的啊, maxwell下的各种卡都是游戏卡,具体可以见: https://developer.nvidia.com/maxwell-compute-architect ...

  5. 使用 BeanCopier 复制对象

    Cglib是一款比较底层的操作java字节码的框架. BeanCopier是一个工具类,可以用于Bean对象内容的复制. 复制Bean对象内容的方法有很多,比如自己手动get set ,或者使用Pro ...

  6. eclipse 的操作

    1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右 侧Text file encodin ...

  7. Windows服务的手动添加和删除方法

    Windows服务的手动添加和删除方法 服务,是指执行指定系统功能的程序.例程或进程,以便支持其他程序,尤其是低层(接近硬件)程序.其实,服务就是一种特殊的应用程序,它从服务启动开始就一直处于运行状态 ...

  8. ASP.NET访问网络映射盘&实现文件上传读取功能

    最近在改Web的时候,遇到一个问题,要跨机器访问共享文件夹,以实现文件正常上传下载功能. 要实现该功能,可以采用HTTP的方式,也可以使用网络映射磁盘的方式,今天主要给大家分享一下使用网络映射磁盘的方 ...

  9. EasyUI 后台接受DataGrid传来的参数

    string ad = Context.Request.QueryString["rows"];不行 string aedf = Context.Request.Form[&quo ...

  10. 去除inline-block之间的间隙

    在使用display: inline-block;时,这些inline-block之间会有间隙,根据无双 - <去除inline-block元素间的空隙>的说法,这个间隙是4px或者8px ...