Pablo Galiano : VS10 Beta 1 / T4 Preprocessing part 2

Subscriptions

<March 2010>
SuMoTuWeThFrSa
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

News

Subscribe to Pablo Galiano by Email

Post Categories

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:

image

After we add a pre-compiled template item, we can see the output of the transformation:

 

image

 

image

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 :

image

 

Pablo

posted on Wednesday, July 15, 2009 7:16 AM by pga

# re: VS10 Beta 1 / T4 Preprocessing part 2 @ Wednesday, July 22, 2009 2:40 AM

Hmm, sounds good.
But is it possible to use this compiled assembly (PreTT.dll) also back in VisualStudio?
for example as a CustomTool? With some simple deployment? :-)

Jeniczek