【问题标题】:Include another t4 template dynamically动态包含另一个 t4 模板
【发布时间】:2016-10-03 16:00:04
【问题描述】:

我正在使用 t4 模板并面临以下问题。我需要在我的模板中包含另一个现有的 t4 模板。但我有一个限制:包含的 t4 模板文件的名称是动态生成的。 我为此使用了include 指令,但它不起作用。

<#@ include file="\Helpers\<# FileName.tt#>" #>

我收到一个错误:

 An unexpected start or end tag was found within a block. Make sure that you did not mis-type a start or end tag, and that you do not have any nested blocks in the template.

属性FileName的值是动态生成的

【问题讨论】:

  • 为什么要生成 T4?我想不出任何附加步骤有意义的场景。
  • @Toxantron,我有一些 t4 模板文件。并且取决于情况,我应该将其中之一包含到我的模板中。但是包含哪一个是由配置文件决定的。
  • 你试过了吗? &lt;#@ include file=string.Format(@"\Helpers\{0}.tt", FileName) #&gt;
  • 是的,但没用。
  • 我认为这里没有办法使用include,因为在我看来,include是在编译时解决的。是运行时模板吗?

标签: c# t4


【解决方案1】:

为了使用参数,您需要设置 msbuild 属性(例如在 BeforeBuild 目标中)或作为 /p:TargetPath="[path]" 传递给 msbuild,您可以将它们与以下内容一起使用:

<#@ include file="$(TargetPath)\TargetFile.tt" #>

(更多关于MSBuild properties usage的信息在这里)

此外,您可以配置 t4 模板以在每个构建上运行

<!-- This line could already present in file. If it is so just skip it  -->
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- process *.tt templates on each build  -->
<PropertyGroup>
    <TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TextTemplating\v10.0\Microsoft.TextTemplating.targets" />

...这里提到:Get Visual Studio to run a T4 Template on every build

【讨论】:

    猜你喜欢
    • 2013-10-18
    • 2012-08-15
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2015-11-30
    相关资源
    最近更新 更多