How to use T4 templates in WP7, Silverlight, Desktop or even MonoDroid apps
- by Daniel Cazzulino
In other words, how to use T4 templates without ANY runtime dependencies? Yes, it is possible, and quite simple and elegant actually. In a desktop project, just open the Add New Item dialog, and search for "text template": From the two available templates, the one that gives you a zero-dependency runtime-usable template is the first one: Preprocessed Text Template. Once unfolded, you get the .tt file, but also a dependent .cs file automatically generated. Note the Custom Tool associated with the file: If you open up the .cs file, you will see that it doesn't contain the rendered "Hello World!!!" I added in the .tt, but rather a full class named after the template file itself: namespace ConsoleApplication1
{
using System;
#line 1 "C:\Temp\ConsoleApplication1\ConsoleApplication1\PreTextTemplate1.tt"
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "10.0.0.0")]
public partial class PreTextTemplate1 : PreTextTemplate1Base
{
public virtual string TransformText()
{
this.GenerationEnvironment = null;
this.Write("Hello World!!!");
return this.GenerationEnvironment.ToString();
}
}
#region Base class
...
#endregion
}
...
Read full article