我正在创建一个 T4 模板,我希望根据某些条件有条件地包含模板。例如,我尝试了以下...
<#switch(iocContainer) {#>
<#case "Autofac":#>
<#@ include file="Autofac\EntityTemplate.ttinclude" #>
<#@ include file="Autofac\ServiceTemplate.ttinclude" #>
<#@ include file="Autofac\RepositoryTemplate.ttinclude" #>
<#@ include file="Autofac\DbContextTemplate.ttinclude" #>
<#break;#>
<#case "Castle":#>
<#@ include file="Castle\EntityTemplate.ttinclude" #>
<#@ include file="Castle\ServiceTemplate.ttinclude" #>
<#@ include file="Castle\RepositoryTemplate.ttinclude" #>
<#@ include file="Castle\DbContextTemplate.ttinclude" #>
<#break;#>
<#case "nInject":#>
<#@ include file="nInject\EntityTemplate.ttinclude" #>
<#@ include file="nInject\ServiceTemplate.ttinclude" #>
<#@ include file="nInject\RepositoryTemplate.ttinclude" #>
<#@ include file="nInject\DbContextTemplate.ttinclude" #>
<#break;
}#>
问题是当模板引擎运行时,它似乎在评估任何代码之前预处理了所有包含。所以上面的 switch 语句没有运行,T4 尝试包含所有文件。
有没有办法有条件地包含 T4 模板?