zoukankan      html  css  js  c++  java
  • T4模版基础例子

    <#@ template debug="false" hostspecific="true" language="C#" #>
    <#@ assembly name="System.Core" #>
    <#@ assembly name="EnvDTE" #>
    <#@ import namespace="System.Linq" #>
    <#@ import namespace="System.Text" #>
    <#@ import namespace="System.Collections.Generic" #>
    <#@ import namespace="System.IO" #>
    <#@ output extension=".txt" #>
    
    <#
      IServiceProvider serviceProvider = (IServiceProvider)this.Host;
      EnvDTE.DTE dte = (EnvDTE.DTE) serviceProvider.GetService(typeof(EnvDTE.DTE));
    #>
    
    
    <#
      string path = Path.Combine(dte.ActiveDocument.Path,"aa.txt");
      string line = string.Empty;
      using(StreamReader reader = new StreamReader(path))
      {
         line = line = reader.ReadToEnd();
      }
    #>
    
    <#=line#>

    上面代码的作用是通过T4模版来读取与模版文件同一目录中的aa.txt的内容,并生成文件到指定的文件中。

    基础语法解释:

    hostspecific="true" ,如果将此特性的值设置为 true,则会将名为 Host 的属性添加到由文本模板生成的类中。 

    EnvDTE相关信息参考,https://msdn.microsoft.com/zh-cn/library/envdte.aspx

    EnvDTE 是包含 Visual Studio 内核自动化的对象和成员的用程序集包装的 COM 库。

  • 相关阅读:
    2-3-4 tree留坑
    CCPC final Cockroaches
    对拍模板
    使用cronolog按日期分割日志
    linux git 命了
    变量加减乘除运算
    根据pom标签修改
    根据符号获取字符
    shell循环字符串数组
    git ssh key配置
  • 原文地址:https://www.cnblogs.com/huangzelin/p/4595440.html
Copyright © 2011-2022 走看看