• T4模板TextTemplatingFileGenerator和TextTemplatingFilePreprocessor区别


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

     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

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

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

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace Client.Templates
     7 {
     8    public  class Util1
     9     {
    10        public static void HelloWorld()
    11        {
    12            Console.WriteLine("ni haoqqqqxxx!");
    13        }
    14 
    15        public static string Method2()
    16        {
    17            return "Method2  from c# code";
    18        }
    19 
    20        public static string  Method3()
    21        {
    22            return "code from c# class Util1 method Method3 ";
    23        }
    24 
    25     }
    26 }
    View Code

    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。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace Client.Templates
     7 {
     8    public  class Util1
     9     {
    10        public static void HelloWorld()
    11        {
    12            Console.WriteLine("ni haoqqqqxxx!");
    13        }
    14 
    15        public static string Method2()
    16        {
    17            return "Method2  from c# code";
    18        }
    19 
    20        public static string  Method3()
    21        {
    22            return "code from c# class Util1 method Method3 ";
    23        }
    24 
    25        public static string Method4()
    26        {
    27            return "code from c# class Util1 method Method4";
    28        }
    29     }
    30 }
    View Code

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

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using Client.Templates;
     6 
     7 namespace Client
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             SecondTemp temp = new SecondTemp();
    14             String pageContent = temp.TransformText();
    15             System.IO.File.WriteAllText("SecondTemp.txt", pageContent);
    16             Console.WriteLine("task done!");
    17             Console.ReadKey();
    18         }
    19     }
    20 }
    View Code

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

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

     

  • 相关阅读:
    HDU 4571 Travel in time(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)
    第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】
    第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】
    二分最化最值问题(二)
    二分最化最值问题(一)
    CONTINUE...?【构造/分析】
    Doki Doki Literature Club
    King of Karaoke
    Peak
    ZOJ18th省赛 Lucky 7
  • 原文地址:https://www.cnblogs.com/huangkaiyan10/p/swan_T4_TextTemplatingFilePreprocessor_TextTemplatingFileGenerator.html
Copyright © 2020-2023  润新知