T4模板引擎 参数调用
1.首先解决语法高亮的问题,下载安装自选版本
https://marketplace.visualstudio.com/items?itemName=tangibleengineeringGmbH.tangibleT4Editor240plusmodelingtoolsforVS2017
2. 使用vs自带的TextTransform运行模板
***参数使用两个感叹号开始,使用单个感叹号作为值开始字段***
```
D:\VS2022\Enterprise\Common7\IDE\TextTransform.exe D:\work\code\test\Test.tt !!ParameterName!st
```
读取传递的参数,需要将t4模板上方的hostspecific="true"设置为true
parameter 指令不检索在 TextTransform.exe 实用工具的 -a 参数中设置的值。 若要获取这些值,在 template 指令中设置 hostSpecific="true",并使用 this.Host.ResolveParameterValue("","","argName")。
```
string parameterTest = Host.ResolveParameterValue(null, null, "ParameterName");
```
T4模板内容如下
```
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>
int a=6;
Console.WriteLine("ParameterName");
<#
string parameterTest = Host.ResolveParameterValue(null, null, "ParameterName");
WriteLine(parameterTest);
#>
Console.WriteLine("<#= parameterTest #>");
```
如果需要在T4模板中使用json,可以在命令行中加入以下两个参数
```
-r "Newtonsoft.Json" -u "Newtonsoft.Json"
```
如果需要包含ttinclude文件则需要通过 -I 参数导入ttinclude文件所在的目录,同时在引入文件内部需要的程序集即可
https://blog.csdn.net/weixin_34220623/article/details/86403990?spm=1035.2023.3001.6557&utm_medium=distribute.pc_relevant_bbs_down_v2.none-task-blog-2~default~OPENSEARCH~Rate-1.pc_relevant_bbs_down_cate&depth_1-utm_source=distribute.pc_relevant_bbs_down_v2.none-task-blog-2~default~OPENSEARCH~Rate-1.pc_relevant_bbs_down_cate
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