How to use T4 templates in WP7, Silverlight, Desktop or even MonoDroid apps

Posted by Daniel Cazzulino on ASP.net Weblogs See other posts from ASP.net Weblogs or by Daniel Cazzulino
Published on Thu, 09 Dec 2010 16:33:59 GMT Indexed on 2010/12/09 22:14 UTC
Read the original article Hit count: 380

Filed under:
|

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":

image

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:

image

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

© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about All Technology