1
我有一個非常奇怪的情況與T4模板。T4子模板TransformText()不工作
我有一個字符串集合,它們代表了要用模板生成的實體。在這裏,我有這樣的代碼:
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ include file="EntityTemplate.tt" #>
<#
// kinda var Names = new List<string>();
foreach (name in Names)
{
EntityTemplate template = new EntityTemplate();
template.EntityClassName = name;
template.PropertyActions = new System.Collections.Generic.List<ConfigActionBase>
{
new DefaultAction,
new ValidateAction
};
ProcessContent(name + ".cs", template.TransformText());
}
#>
DEFAULTACTION和ValidateAction從Microsoft.VisualStudio.TextTemplating.TextTransformation繼承和覆蓋TransformText()方法是這樣的:
<#+public class ValidateAction: Microsoft.VisualStudio.TextTemplating.TextTransformation
{
public override string TransformText()
{#>
public void Foo()
{ throw new NotImplementedException(); }
<#+return GenerationEnvironment.ToString(); // BREAKPOINT HERE
}
}#>
而且ProcessContent()的方法是:
public void ProcessContent(string outputFileName, string content)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, outputFileName);
File.WriteAllText(outputFilePath, content);
}
這裏是我的EntityTemplate類:
<#+
public class EntityTemplate : Microsoft.VisualStudio.TextTemplating.TextTransformation
{
public string EntityClassName { get; set; }
public System.Collections.Generic.List<Microsoft.VisualStudio.TextTemplating.TextTransformation> PropertyActions { get; set; }
public override string TransformText()
{#>
namespace MyGenerated
{
public class <#= EntityClassName #>
{
<#+foreach (var action in PropertyActions)
action.TransformText();#>
}
}
<#+return this.GenerationEnvironment.ToString();
}
}#>
所以,問題是我的模板只生成類名和空括號,並且沒有任何操作生成代碼(Foo方法)。正如我們所看到的,Actions是子模板。但是,如果我將斷點插入到評論爲「BREAKPOINT HERE」的行中,我會在GenerationEnvironment對象中看到所有需要的代碼。如果我引入單獨的變量並返回它的值,問題仍然存在:值已設置,但沒有任何呈現輸出。
任何想法? :)