VS10 Beta 1 / T4 Preprocessing part 2
***Disclaimer: This information applies to Visual Studio 2010 Beta 1 only.***
In the first part I covered the details of the T4 Preprocessing API.
Now it’s time to create a preprocessed text template and mimic the GAX property directive processor feature.
Let’s add a preprocessed text template. For that purpose there is a new item template in the VS10 Add new item dialog:
After we add a pre-compiled template item, we can see the output of the transformation:
As you can see, the namespace is calculated from the project default namespace property, and the tt item name is used to calculate the class name.
Using properties to mimic the GAX PropertyProcessor directive
After the TemplatedPreprocessor runs we end up with a template class, which is just a regular class with some helper methods to write the transformation.
These methods are similar to the ones provided in the base TextTransformation class. But to remove the dependency to the Microsoft.VisualStudio.TextTemplating.10.0 assembly those methods are now part of our template class.
To add properties that will be consumed inside the template, it is just a matter of declaring a partial class and adding them there:
namespace PreTT
{
public partial class MyPreTextTemplate
{
public string MyProperty { get; set; }
}
}
Then from within the template we can just refer to the property:
<#@ template language="C#" #>
MyProperty value is <#= this.MyProperty #>
To test the transformation we create an instance of the template class and we call the TransformText method:
public static void Main(string[] args)
{
Console.WriteLine(
new MyPreTextTemplate { MyProperty = "Foo" }
.TransformText());
Console.Read();
}
And the transformation output is :
Pablo