【问题标题】:T4 templates generates unwanted output fileT4 模板生成不需要的输出文件
【发布时间】:2012-10-06 05:20:03
【问题描述】:

我有一个名为 ParentTemplate 的 T4 模板,其中包括另一个名为 ChildTemplate 的 T4 模板。这个想法是调用我的 ParentTemplate 并为每次调用 ChildTemplate 生成一个文件。但是,我的 ParentTemplate 还创建了一个我不需要的文件 (.cs)。 ParentTemplate 的 Build Action 设置为“None”,Custom Tool 设置为“TextTemplatingFileGenerator”。以下代码来自 ParentTemplate:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ import namespace="System.IO" #>
<#
    for (int i = 0; i < 3; i++)
    {
#>
<#@ include file="ChildTemplate.t4" #>
<#
    string filename = Path.Combine(path, String.Format("{0}i.txt", myFile));
    File.WriteAllText(filename, this.GenerationEnvironment.ToString());
    this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
    }
#>

在 ChildTemplate 中只有文本“Hello World”。所以我想要的输出是 3 个 .txt 文件,内容是“Hello World”,就是这样。所以没有像 ParentTemplate.cs 这样的其他文件。

知道如何防止 ParentTemplate.cs 的生成吗?

【问题讨论】:

    标签: templates t4


    【解决方案1】:

    只需清除Custom Tool 属性值即可。

    【讨论】:

      【解决方案2】:

      here 表示您可以执行以下操作来欺骗 Visual Studio,使其不为使用生成器的 .tt 文件生成输出文件。

      <#@ output extension="/" #>
      

      <#@ output extension="\\" #>
      

      注意:这是一个 hack,仍然会产生警告。
      注意:清除 Custom Tool 属性值不会达到您想要的效果,因为将完全避免运行模板。

      您还可以采取更迂回(且耗时)的方法:

      • 在生成过程中使用DTEEnv 从 VSProject 中删除文件(仅在 Visual Studio 中运行时可用)。
      • 生成后使用构建任务移除和删除项目(使用MSBuild)。

      【讨论】:

        猜你喜欢
        • 2021-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多