是否有人知道是否可以在VS2010中从另一个T4模板运行T4模板文件
谢谢
发布于 2010-09-03 21:20:42
可以,停那儿吧。我是这样做的:
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); 发布于 2013-01-03 06:41:14
您可能要找的是T4 Toolbox。它将允许您在单个文件中实际生成代码,并自动将它们添加到项目中。
强烈推荐。
我已经使用t4工具箱仅仅基于一个模型来生成整个项目。
(T4工具箱以前位于http://t4toolbox.codeplex.com/,但现在位于https://github.com/olegsych/T4Toolbox )
发布于 2010-11-13 03:29:16
我们经常这样做。下面是一个例子,我们如何重用一个通用的T4模板,同时“传递参数”到它里面:
<#
var currentUsername = "billclinton"; // this is for integration tests impersonating a user in our case
#>
<#@ include file="..\SomeTemplateThatIWantToReuseHere.tt" #>我们通过动态确定T4模板实际运行的位置(在本例中是包含include行的T4模板)来保持我们的T4模板“通用”:
string namespaceName = code.VsNamespaceSuggestion();
var namespaceParts = namespaceName.Split('.');
var currentNamespaceLeg = namespaceParts.Last();这允许我们做一些非常强大的模板,而不需要复制我们的模板。唯一被“复制”的是包含include调用的4行.tt文件,但这些文件几乎不需要维护,除非我们想通过以我们的方式传递变量来执行任何“配置”。
https://stackoverflow.com/questions/3636094
复制相似问题