t4 - 从 TextTransform.exe 获取参数值到模板中

标签 t4 template-engine

当我使用 TextTransform.exe 从模板生成代码时,我找不到一些示例如何使用参数 -a。在 MSDN以下是参数 -a 的描述:

"Specifies a parameter that a directive processor can query for as a name/value pair. The directive processor and identifier are optional. This allows parameters to be specified for any directive processor or any instance of a particular directive processor."



我的模板中需要一些参数,如连接字符串等。我的想法是在参数 -a 的帮助下获取配置文件的路径。

问候
安东·卡尔奇克

更新:为了清楚起见,我想直接在模板中读取参数。

最佳答案

Text Template Transformation Toolkit(T4) 来自 Microsoft 的支持不是很好。只有几个例子。如果您想了解更多,请访问 Olegs Sychs blog . T4在这里解释得非常深入。

经过数小时尝试从模板中的 TextTransform.exe 获取参数后,我找到了一个解决方案:

添加 hostspecific="true"归属于 template元素如下:

<#@ template language="C#v3.5" hostspecific="true"#>

稍后在模板中您可以调用 ResolveParameterValue正如奥列格所说。

例子:
<#

 string parameterTest = Host.ResolveParameterValue(null, null, "someKey");
 WriteLine(parameterTest);

#>

你这样调用模板生成器:
"C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.2\TextTransform.exe" -a !!someKey!someValue

生成后应该在生成的文件中:'someValue'

关于t4 - 从 TextTransform.exe 获取参数值到模板中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1221651/

相关文章:

node.js - 如何将一个部分包含到另一个部分的 block 中?

linq - t4 模板 linq 问题

go - 直接从 Revel 模板引擎中拉取模型

javascript - 在 JADE 中完成 if-else 条件 block

c# - 从 CodeAttribute 中的参数获取 CodeClass?

javascript - 更改模板标签后 John Resig 的微模板出现语法错误 <# {% {{ 等

java - 将 JSON 对象传递给 Rythm 模板

visual-studio-2013 - 在 VS2013 T4 脚手架模板中获取自定义属性

c# - 如何在 T4 模板中使用 resx 资源文件

entity-framework - InvalidOperationException(集合已设置为 EntityCollection)在模板修改后所有属性都是虚拟的