Mono.TextTemplating将参数传递给模板

时间:2013-02-05 15:34:41

标签: c# monodevelop t4 css texttemplate

我尝试使用以下命令将参数(foo)传递给现有模板(MyTemplate.tt):

TextTransform.exe -a = foo!bar -o Output.txt MyTemplate.tt

MyTemplate.tt:

<#@ template language="C#" #>
<#@ import namespace="System.IO" #>

<#
  this.Write(foo);  
#>

由于参数“foo”不存在,此命令失败。 如何正确传递此参数?

2 个答案:

答案 0 :(得分:3)

MonoDevelop的TextTransform.exe的参数与Microsoft的匹配,记录为here

不幸的是,这些参数不像代码中的变量那样公开,它们作为自定义指令处理器的参数。要直接从代码访问它们,您必须设置hostspecific="true"并通过Host.ResolveParameterValue (paramName)访问它们。

然而,您可以使用动态对象通过模板基类上的implementing IDynamicMetaObjectProvider并覆盖BindGetMember来解析Host.ResolveParameterValue中的参数。

答案 1 :(得分:0)

谢谢,这对我来说很好用:

TextTransform.exe -a = foo!bar -o Output.txt MyTemplate.tt

MyTemplate.tt:

<#@ template language="C#" hostspecific="true" #>
<# string temp = this.Host.ResolveParameterValue("", "", "foo");#>
<#
  this.Write(temp);  
#>

Output.txt的:

bar

我还要看一下IDynamicMetaObjectProvider。

相关问题