gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:46:06 27 4
gpt4 key购买 nike

当我使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com