我尝试按照本教程进行操作:http: //t4-editor.tangible-engineering.com/blog/how-to-generate-multiple-output-files-from-a-single-t4-template.html
使用视觉工作室 2015 (.Net 4.5)
有错误的示例项目:http ://www.filedropper.com/t4fail
我使用以下来源创建了 Template1.tt:
<#@ include file="TemplateFileManagerV2.1.ttinclude" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="System.Windows.Forms" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#
var manager = TemplateFileManager.Create(this);
#>
我TemplateFileManagerV2.1.ttinclude
从模板库添加到我的项目中。
然后我得到一个错误:
“Microsoft.VisualStudio.TextTemplating.IDebugTextTemplatingEngine”在未引用的程序集中定义。您必须添加对程序集“Microsoft.VisualStudio.TextTemplating.Interfaces.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”的引用。
所以我添加了对
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.11.0\v4.0_11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.11.0.dll
和
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.Interfaces.11.0\v4.0_11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.Interfaces.11.0.dll
到我的项目,但没有任何改变。
错误在下面的方法里面.ttinclude
public string GetTemplateContent(string templateName, TextTemplatingSession session)
{
string fullName = this.Host.ResolvePath(templateName);
string templateContent = File.ReadAllText(fullName);
var sessionHost = this.Host as ITextTemplatingSessionHost;
sessionHost.Session = session;
Engine engine = new Engine();
return engine.ProcessTemplate(templateContent, this.Host);
}
我把它换成了
public string GetTemplateContent(string templateName, TextTemplatingSession session)
{
string fullName = this.Host.ResolvePath(templateName);
string templateContent = File.ReadAllText(fullName);
var sessionHost = this.Host as ITextTemplatingSessionHost;
sessionHost.Session = session;
//Engine engine = new Engine();
return "";//engine.ProcessTemplate(templateContent, this.Host);
}
检查问题是否确实在 dll 中并得到:
'Microsoft.VisualStudio.TextTemplatingA30AC8B57EFC4307E43667FCD72F5E4857F498C5224AE0D43FFC74B3A98D4FA090794EF196648D62B1BC664AFBA5EDE831067D7D1768A759EBBE83426975F7AA.GeneratedTextTransformation' does not contain a definition for 'Host' and no extension method 'Host' accepting a first argument of type 'Microsoft.VisualStudio.TextTemplatingA30AC8B57EFC4307E43667FCD72F5E4857F498C5224AE0D43FFC74B3A98D4FA090794EF196648D62B1BC664AFBA5EDE831067D7D1768A759EBBE83426975F7AA.GeneratedTextTransformation' could be found (are you missing a using directive还是汇编参考?)
看来,并非如此。