I have a very strange situation with T4 templates.
I have a collection of strings which are representing entities to be generated with a template. Here I have such code:
<#@ 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 and ValidateAction are inherited from Microsoft.VisualStudio.TextTemplating.TextTransformation and override TransformText() method like this:
<#+public class ValidateAction: Microsoft.VisualStudio.TextTemplating.TextTransformation
{
public override string TransformText()
{#>
public void Foo()
{ throw new NotImplementedException(); }
<#+return GenerationEnvironment.ToString(); // BREAKPOINT HERE
}
}#>
And ProcessContent() method is:
public void ProcessContent(string outputFileName, string content)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, outputFileName);
File.WriteAllText(outputFilePath, content);
}
And here is my EntityTemplate class:
<#+
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();
}
}#>
So, the problem is that my template generating only class name and empty braces and no any actions generated code (Foo methods). As we see, Actions are subtemplates. But if I insert breakpoint to the line commented "BREAKPOINT HERE" I see all needed code inside GenerationEnvironment object. And if I introduce separate variable and return try its value, the problem still here: value is set, but nothing is rendered to output.
Any ideas? :)