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