【问题标题】:Get Project or Relative Directory with T4使用 T4 获取项目或相对目录
【发布时间】:2010-11-24 01:35:15
【问题描述】:

如何从 t4 模板中获取对 Visual Studio 项目或解决方案目录或 t4 模板目录的引用?

我有一个模板,它将多个文件连接在一起,这些文件相对于模板定位。我需要通过相对方式获得对那里的绝对位置的引用。硬编码绝对路径是不可接受的,因为 Visual Studio 项目并不总是在同一个位置,这会破坏代码生成。从环境中读取当前工作目录也不起作用,因为这会返回 T4 引擎所在的位置而不是模板。我刚刚进入 t4,所以任何帮助将不胜感激。

【问题讨论】:

    标签: visual-studio visual-studio-2008 file-io code-generation t4


    【解决方案1】:

    请参阅主机特定参数部分HERE

    这个 sn-p 展示了如何获取 src 相对于模板的完整路径。

    <#@ template hostspecific="true" #>
    // The location of the src folder relative to the t4 template:
    // <#= Host.ResolvePath("src") #>
    

    【讨论】:

      【解决方案2】:

      你也可以这样抓取路径

      <#@ template hostspecific="true" #>
      <#= Path.GetDirectoryName(this.Host.TemplateFile) #>
      

      【讨论】:

      • 还需要导入 Path 的命名空间:&lt;#@ import namespace="System.IO" #&gt;
      猜你喜欢
      • 2011-01-04
      • 2015-04-30
      • 2021-04-06
      • 2011-01-06
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      相关资源
      最近更新 更多