T4模版基础例子
2015-06-23 14:44
483 查看
<#@ 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 库。
相关文章推荐
- InCAM与Genesis的综合比较
- SSM(Spring+SpringMVC+MyBatis)整合
- bjective-C 中核心处理字符串的类是 NSString 与 NSMutableString
- [leetcode] Contains Duplicate III
- 你觉得sql有多难?
- 很多网站301重定向
- 如何制作微信表情
- python简单网络服务器
- MySQL Group By的优化
- jave应用
- spring-data-jpa
- TCP连接表1
- commit already called
- c/c++如何相互调用
- SATA 学习笔记
- 深入分析Java Web技术内幕_第二章 学习笔记
- JavaScript instanceof 的用法
- kill 命令参数+cenos安装图形界面
- Oracle 10G RAC 删除已有节点
- 2015年中国移动云计算平台市场发展趋势分析