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

问题描述 投票:5回答:1

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

c# .net t4
1个回答
4
投票

你可以使用RenderToFilet4Toolbox来做到这一点。

截至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();
        }
    }
#>

Original Documentation

Wayback Machine

© www.soinside.com 2019 - 2024. All rights reserved.