c# – 如何使用有形编辑器从单个T4模板创建多个输出文件?
我试着按照这个教程:
http://t4-editor.tangible-engineering.com/blog/how-to-generate-multiple-output-files-from-a-single-t4-template.html
与visual studio 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到我的项目. 然后我收到一个错误:
所以我添加了引用
和
我的项目,但没有任何改变. 错误在.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中并得到:
看来,事实并非如此. 解决方法
我记得我在2010年找到了一种更简单的方法,但现在,在浏览了该方法的网页之后,我再也找不到了.因此,经过一番挖掘,我设法在一个旧的源代码库中找到它.这是我当时的做法,没有使用任何外部文件或依赖:
<#@ template debug="false" hostspecific="true" language="C#" #> <#@ assembly name="System.Core" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ output extension=".txt" #> <# for (Int32 i = 0; i < 10; ++i) { #> Content <#= i #> <# // End of file. SaveOutput("Content" + i.ToString() + ".txt"); } #> <#+ private void SaveOutput(string outputFileName) { string templateDirectory = Path.GetDirectoryName(Host.TemplateFile); string outputFilePath = Path.Combine(templateDirectory,outputFileName); File.WriteAllText(outputFilePath,this.GenerationEnvironment.ToString()); this.GenerationEnvironment.Remove(0,this.GenerationEnvironment.Length); } #> 请注意,我不知道这个方法的原作者是谁.如果您碰巧知道他或她是谁,请发表评论. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |