使用 T4,我想根据检查相对于正在执行的模板文件的目录中的文件来生成一些代码。
c#中有没有办法识别当前模板文件的路径是什么?
hostspecific="true"
属性设置为.<#@ template
True
Host
,它使您可以访问ResolvePath
方法和TemplateFile
属性。
Host
是类型ITextTemplatingEngineHost
。TemplateFile
是一个String
通常是文件的文件系统路径的值.tt
- 但是其他 T4 主机(即除 Visual Studio 之外的主机)可能会在内存中加载 T4 文件,或者可能返回一些其他值,因为模板文件没有存在于磁盘上。例如:
<#@ template hostspecific="true" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
ITextTemplatingEngineHost t4Host = this.Host;
FileInfo t4FileInfo = new FileInfo( t4Host.TemplateFile );
#>
// This file generated by <#= t4FileInfo.FullName #>