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

从其他T4模板运行T4模板

  •  7
  • muek  · 技术社区  · 14 年前

    有人知道在VS2010中是否可以从另一个T4模板运行T4模板文件吗

    谢谢

    4 回复  |  直到 14 年前
        1
  •  8
  •   RichardTowers    11 年前

    是的,你可以。我就是这么做的:

    string templateText = File.ReadAllText(Host.ResolvePath(templateFileName));
    Engine engine = new Engine();
    string output = engine.ProcessTemplate(templateText, Host);
    //this is optional - record all output to your file of choice:
    File.WriteAllText(outputFilePath, output); 
    
        2
  •  3
  •   Dai    4 年前

    你可能要找的是 T4 Toolbox . 它将允许您在单个文件中实际生成代码,并自动将它们添加到项目中。

    强烈推荐。

    我使用t4工具箱来生成基于模型的整个项目。

    (T4工具箱以前位于 http://t4toolbox.codeplex.com/ https://github.com/olegsych/T4Toolbox )

        3
  •  1
  •   Jaxidian    14 年前

    我们经常这样做。下面是一个例子,我们如何重用一个常见的T4模板,但“传递参数”到其中:

    <#
    var currentUsername = "billclinton"; // this is for integration tests impersonating a user in our case
    #>
    <#@ include file="..\SomeTemplateThatIWantToReuseHere.tt" #>
    

    include 行(在其中):

    string namespaceName = code.VsNamespaceSuggestion();
    var namespaceParts = namespaceName.Split('.');
    var currentNamespaceLeg = namespaceParts.Last();
    

    这允许我们做一些非常强大的模板,而不需要复制我们的模板。唯一被“复制”的是我们的4线 .tt 包括

        4
  •  0
  •   Oleg Sych    14 年前

    有多个光学元件具有不同的权衡:

    http://www.olegsych.com/2008/04/t4-template-design/