从 TextTransform.exe 获取参数值到模板中 [英] Get argument value from TextTransform.exe into the template

查看:27
本文介绍了从 TextTransform.exe 获取参数值到模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

"指定一个参数,一个指令处理器可以查询为名称/值对.该指令处理器和标识符是可选的.这允许指定参数对于任何指令处理器或任何特定指令的实例处理器."

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

问候安东·卡尔奇克

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

解决方案

Text Template Transformation Toolkit(T4) 来自 Microsoft,支持度不高.只有几个例子.如果您想了解更多信息,请访问 Olegs Sychs 博客.T4在这里解释得非常深入.

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

hostspecific="true" 属性添加到 template 元素,如下所示:

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

稍后在模板中,您可以像 Oleg 提到的那样调用 ResolveParameterValue.

示例:

<预><代码><#string parameterTest = Host.ResolveParameterValue(null, null, "someKey");写线(参数测试);#>

你这样调用模板生成器:

"C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.2\TextTransform.exe" -a !!someKey!someValue

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

I can't found some example how can I use argument -a when I use TextTransform.exe to generate code from templates. In MSDN is following description for argument -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."

I need some set of arguments like connection string and so on in my template. My idea was to get a path to configuration file with help of argument -a.

Regards Anton Kalcik

UPDATE: To be clear enough, I want read parameters direct in template.

解决方案

Text Template Transformation Toolkit(T4) is from Microsoft not very well supported. Only few examples. If you want to know more go to Olegs Sychs blog. T4 is here very deeply explained.

After of hours to trying to get parameters from TextTransform.exe in my template I found a solution:

Add hostspecific="true" attribute to template element as follows:

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

Later in template you can call ResolveParameterValue as Oleg mentioned.

Example:

<#

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

#>

You call template generator so:

"C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.2\TextTransform.exe" -a !!someKey!someValue

After generating should be in generated file: 'someValue'

这篇关于从 TextTransform.exe 获取参数值到模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆