我正在嘗試編寫一個控制檯程序,它將T4模板的文件名作爲參數,然後處理該模板。這是因爲我希望用戶能夠更新模板而不必重新編譯程序。執行T4模板:主機爲null,而hostspecific =「True」
,我發現最簡單的方法是創建一個預編譯的C#類,後者又執行我的外部模板「Template.tt」 Visual Studio中第二個模板:
<#@ template language="C#" hostspecific="True" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
string template = File.ReadAllText(Host.ResolvePath("Template.tt"));
Engine engine = new Engine();
string output = engine.ProcessTemplate(template, Host);
Write(output);
#>
內的Program.cs我的控制檯程序,我然後執行此「呼叫」模板:
string templateText = new Caller().TransformText();
問題是,當我運行這個(F5),灑在我的來電顯示模板中的第一行一個NullReferenceException,因爲主機爲null。
但是,我認爲將hosts設置爲True會給我一個主機。
我需要做什麼來獲得主機?真的不想要推出自己的。 或者,有沒有更好的方法來執行外部T4模板?