如何使用.tt模板创建文件夹?

Mik*_*keW 5 .net c# t4

我正在尝试构建一个模板,它将在不同的文件夹中创建一系列文件,但我还没有找到任何样本.

kry*_*our 4

您可以使用t4ToolboxRenderToFile来执行此操作。

截至 2016 年 10 月 12 日的文档示例片段:

  • 使用两个 C# 类库项目 ClassLibrary1.csproj 和 ClassLibrary2.csproj 创建 Visual Studio 解决方案。

  • 将名为 CodeGenerator.tt 的新代码生成文件添加到第一个类库项目中。

  • 修改新文件的内容,如下所示

<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#
    SampleTemplate template = new SampleTemplate();
    template.Output.File = @"SubFolder\SampleOutput.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();
        }
    }
#>
Run Code Online (Sandbox Code Playgroud)

原始文档

回溯机