C# T4模板多输出
我想使用另一个t4模板来提供输入,而不是这个。可能吗C# T4模板多输出,c#,t4,C#,T4,我想使用另一个t4模板来提供输入,而不是这个。可能吗 <#@ template language="C#" hostspecific="True" debug="True" #> <#@ output extension="txt" #> <#@ include file="T4Toolbox.tt" #> Sample Content <# for(int i=7;i<9;i++){ SampleTemplate
<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
Sample Content
<# for(int i=7;i<9;i++){
SampleTemplate template = new SampleTemplate();
template.Output.File = @"SubFolder\SampleOutput"+i+".txt";
template.Output.Project = @"..\ClassLibrary2\ClassLibrary2.csproj";
template.Render();
}
#>
<#+
public class SampleTemplate : Template
{
public override string TransformText()
{
this.WriteLine("Hello, World!");
return this.GenerationEnvironment.ToString();
}
}
#>
样本含量
制作一个T4函数,将当前模板输出重定向到指定的文件-另存为SaveOutput.tt:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ import namespace="System.IO" #>
<#
void SaveOutput(string outputFileName)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, outputFileName);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
#>
生成输出代码时,调用以下函数:
<#@ include file="SaveOutput.tt" #>
<#
GenerateFile1();
SaveOutput("File1.txt");
GenerateFile2();
SaveOutput("File2.txt");
#>
<#
void GenerateFile1()
{
#>
This is file 1
<#
}
void GenerateFile2()
{
#>
This is file 2
<#+
}
#>
这是文件1
这是文件2
注意:我从他的博客上获取了这段代码,因为他在他的博客上发布了一个现在已经死了的链接,显示了回答这个问题所需的步骤。因为SO不喜欢只链接的答案,所以我重新发布了那篇博文中的相关代码。你可以找到一份。唯一的区别是,我在每个T4代码块启动后删除了“+”字符-它们似乎不是必需的