簡體   English   中英

從其他 T4 模板運行 T4 模板

[英]Running T4 templates from other T4 template

有誰知道是否可以在 VS2010 中從另一個 T4 模板運行 T4 模板文件

謝謝

是的,你可以。 這就是我的做法:

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); 

您可能正在尋找的是T4 Toolbox 它將允許您在單個文件中實際生成代碼並自動將它們添加到項目中。

強烈推薦。

我使用 t4 工具箱僅基於模型生成整個項目。

(T4 Toolbox 以前位於http://t4toolbox.codeplex.com/但現在位於https://github.com/olegsych/T4Toolbox

我們經常這樣做。 以下是我們如何重用通用 T4 模板但“傳遞參數”到其中的示例:

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

我們通過動態確定 T4 模板實際運行的位置來保持我們的 T4 模板“通用”(在這種情況下,T4 模板中有include行):

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

這允許我們做一些非常強大的模板,而無需復制我們的模板。 唯一被“復制”的是我們的 4 行.tt文件,其中include調用,但是除了我們想要通過以我們的方式傳遞變量來執行的任何“配置”之外,這些文件幾乎是免維護的。

有多個選項具有不同的權衡:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM