代码之家  ›  专栏  ›  技术社区  ›  urig

以编程方式访问T4模板

t4
  •  2
  • urig  · 技术社区  · 15 年前

    <#@ template debug="true" language="C#" hostSpecific="true" #>
    <#@ output extension=".config" #>
    <?xml version="1.0" encoding="UTF-8"?>
    
    <configuration>
        <!-- yadda yadda yadda -->
    </configuration>
    

    我可以从T4工具箱生成器类以编程方式访问此模板吗?我需要这样的东西:

    <#@ include file="web.tt" #>
    <#+
    // <copyright file="Generator1.tt" company="Microsoft">
    //  Copyright © Microsoft. All Rights Reserved.
    // </copyright>
    
    public class Generator1 : Generator
    {
        protected override void RunCore()
        {
            string[] environmentNames = new string[] { "env1", "env2", "env3" };
            foreach (string environmentName in environmentNames)
            {
                Template webTemplate = // programmatically fetch template in defined in web.tt above.
                webTemplate.EnvironmentName = environmentName;
                webTemplate.RenderToFile(environmentName);
            }
        }
    }
    #>
    

    2 回复  |  直到 15 年前
        1
  •  1
  •   Oleg Sych    15 年前

    下面的文章将演示如何对T-SQL存储过程执行此操作。

    http://www.olegsych.com/2008/09/t4-tutorial-creating-reusable-code-generation-templates/

    运行发电机的核心。

    希望这有帮助,

    奥列格

        2
  •  1
  •   Kirk Woll    15 年前

    模板有TransformText()方法,您可以调用该方法以编程方式生成文件。

     Template webTemplate = // programmatically fetch template in defined in web.tt above.
     webTemplate.EnvironmentName = environmentName;
     string output = webTemplate.TransformText();
    
    推荐文章