2016-06-08 105 views
1

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

<#@ 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值是动态

+0

为什么你会产生T4 ?我想不出任何其他增加步骤有意义的场景。 – Toxantron

+0

@Toxantron,我有几个t4模板文件。取决于具体情况,我应该将其中的一个添加到我的模板中。但是哪一个将被包含在配置文件中。 – Mykhalik

+0

你试过了吗? '##include file = string.Format(@“\ Helpers \ {0} .tt”,FileName)#>' – Toxantron

回答

0

生成为了使用一个参数,你需要设置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