T4动态包含指令

时间:2016-03-04 21:57:49

标签: t4

我使用T4生成一些脚本作为运行时模板。其中一部分是读取一些文本文件,其中包含一些T4指令,每个文件使用主模板生成输出。

每个文件可能包含以下文字:

Create table foo (
    rid bigint identity(<#= db.DefaultSeed #>, <#= db.DefaultIncrement #>),
    ...other table specific stuff

我有一个模板,其中包含其他文件中引用的数据库的对象数据和一些源文件引用。

要运行的主要模板可能是:

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ include file="Src" #>

其中src指的是传递给模板的变量。 然后我想有一个文件的字符串列表,可以迭代来构建单个脚本,以便可以在控制台程序中完成以下操作:

public void BuildTables(){
    List<string> files = getFiles();//just get a list of files in a folder to process
    foreach(var f in files){
        var mainTemplate = new MainTemplate(dbSettings);
        mainTemplate.Src = f;
        String text = mainTemplate.TransformText();
        System.IO.File.WriteAllText(outPath + f, text);
    }
}

然而,看起来include指令无法接受如下变量:

<#@ include file="<#= Src #>" #>

有没有办法将文本注入模板文件?

0 个答案:

没有答案
相关问题