Using T4 templates with Silverlight

Everyone who has Visual Studio 2008 also has T4 templates that can be used to generate code, or anything else textual for that matter. Using T4 should be pretty easy but unfortunately Visual Studio kind of hides the fact, there is no “Add T4 template” option and when you manually add one there is no help whatsoever in the box to create a working template. There are some tools out there that will help some but getting started with a simple template is easy. The following is a simple template to create a simple class

   1: <#@ Template language="C#" #>
   2: <#@ import namespace="System.Collections.Generic" #>
   3: <#
   4:     Dictionary<string, string> props = new Dictionary<string, string>();
   5:     props.Add("FirstName", "string");
   6:     props.Add("LastName", "string");
   7:     props.Add("Age", "int");
   8: #>
   9: namespace T4Test
  10: {
  11:     public class Demo
  12:     {
  13: <#
  14:     foreach (KeyValuePair<string, string> prop in props)
  15:     {
  16: #>
  17:  
  18:         /// <summary>
  19:         /// Get or set the <#= prop.Key #> property.
  20:         /// </summary>
  21:         public <#= prop.Value #> <#= prop.Key #> { get; set; }
  22: <#
  23:     }
  24: #>
  25:     }
  26: }


The template looks nice, specially with the keyword highlighting but unfortunately that is my code snippet for Live Writer at work because Visual Studio only shows this as a single color text. The result is a nice C# class that looks like this:



   1: namespace T4Test
   2: {
   3:     public class Demo
   4:     {
   5:  
   6:         /// <summary>
   7:         /// Get or set the FirstName property.
   8:         /// </summary>
   9:         public string FirstName { get; set; }
  10:  
  11:         /// <summary>
  12:         /// Get or set the LastName property.
  13:         /// </summary>
  14:         public string LastName { get; set; }
  15:  
  16:         /// <summary>
  17:         /// Get or set the Age property.
  18:         /// </summary>
  19:         public int Age { get; set; }
  20:     }
  21: }


Pretty easy to do and a useful technique don’t you think so?



 



The bad news



So that was rather easy and great for generating business entities with a bunch of properties, all with identical property implementations. And that is exactly what I tend to do in my Silverlight line of business applications. So I decided to try using T4 with a Silverlight project. But unfortunately that didn’t quite work.  using exactly the same template I only had the single word “ErrorGeneratingOutput” appear in the output.



The errors where a bit more useful thought.



Error    1    Compiling transformation: The type or namespace name ‘CompilerError’ does not exist in the namespace ‘System.CodeDom.Compiler’ (are you missing an assembly reference?)   



Error    2    Compiling transformation: The type ‘System.CodeDom.Compiler.CompilerErrorCollection’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′.   



It turns out Visual Studio uses the project references itself to run the template. And While the System.CodeDom.Compiler namespace exists in Silverlight the required classes like CompilerError and CompilerErrorCollection do not.



This really is a shame as there is really no good reason that T4 templates do not work for Silverlight. All generation is done in Visual Studio, which has access to the full .NET framework, and not at runtime in the browser.



Still enjoy T4 in the full framework.



[f1]
[2]



And now on Twitter at http://www.twitter.com/mauricedb.

5 thoughts on “Using T4 templates with Silverlight

  1. Seems to be working with Visual T4 Professional Edition installed, using our host (Clarius). I just tried on a Silverlight Application and got this:
    namespace T4Test
    {
    public class Demo
    {
    ///

    /// Get or set the FirstName property.
    ///

    public string FirstName { get; set; }
    ///

    /// Get or set the LastName property.
    ///

    public string LastName { get; set; }
    ///

    /// Get or set the Age property.
    ///

    public int Age { get; set; }
    }
    }

    We changed a little how that works, so that may be the reason.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>