0
点赞
收藏
分享

微信扫一扫

T4模板引擎 参数调用

1.首先解决语法高亮的问题,下载安装自选版本

https://marketplace.visualstudio.com/items?itemName=tangibleengineeringGmbH.tangibleT4Editor240plusmodelingtoolsforVS2017

 

 

2.  使用vs自带的TextTransform运行模板

除了直接在vs中通过保存的方式运行模板,还可以通过使用vs安装目录下的TextTransform.exe来执行t4模板

例如

```

D:\VS2022\Enterprise\Common7\IDE\TextTransform.exe D:\work\code\test\Test.tt

```

 

3. 参数

使用TextTransform.exe时可以通过-a参数来传递参数,多个参数可以使用多个-a参数传递

***参数使用两个感叹号开始,使用单个感叹号作为值开始字段***  

```

 -a   !!ParameterName!st

```

 

另外读取传递的参数,需要将t4模板上方的hostspecific="true"设置为true

 

​根据官方文档,t4模板内parameter​​​ 指令不检索在 ​​TextTransform.exe​​​ 实用工具的 ​​-a​​​ 参数中设置的值,若要获取这些值,在 ​​template​​​ 指令中设置 ​​hostSpecific="true"​​​,并使用 ​​this.Host.ResolveParameterValue("","","argName")​​。

例如

```

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

```

 

如果需要在T4模板中使用json,可以在命令行中加入以下两个参数

```

-r "Newtonsoft.Json"  -u "Newtonsoft.Json"

```

 

如果需要包含ttinclude文件则需要通过 -I 参数导入ttinclude文件所在的目录,同时在引入文件内部需要的程序集即可

 其他功能类建议定义在ttinclude文件中,如果要引入在t4文件中需要将定义放在文件最后

```

<#+ 类功能控制块 #> 

```

 

### 调试

设置调试模式

<#@ template debug="true" hostspecific="true" language="C#" #>

添加下列代码

System.Diagnostics.Debugger.Launch();

 

 

### 引用dll  相对路径

1. <#@ assembly name="$(TargetDir)\DLLS\Test.dll" #>

2. <#@ import namespace="Test.Util" #>

 

***需要使用-P参数传递dll所在路径才能正常执行,另外可以把VS中的t4文件属性中的自定义工具删除就可以解决VS找不到dll报错的问题***

 

### 路径空格问题可以使用base64的方式处理,system.texing无需引用其他dll即可在t4文件中得到支持

 

[引用]

 

​​https://docs.microsoft.com/zh-cn/visualstudio/modeling/writing-a-t4-text-template?view=vs-2022​​

 

​​https://docs.microsoft.com/zh-cn/visualstudio/modeling/generating-files-with-the-texttransform-utility?view=vs-2022​​

 

​​https://devblogs.microsoft.com/devops/the-visual-studio-modeling-sdk-is-now-available-with-visual-studio-2017/​​

 

​​https://docs.microsoft.com/zh-cn/visualstudio/modeling/generating-files-with-the-texttransform-utility?view=vs-2017​​

留待后查,同时方便他人



举报

相关推荐

0 条评论