Developing on Staxmanade

T4 Text Templating in Silverlight (Thanks Oleg Sych!)

First if you're new text templating, please go read Oleg Sych's blog. This, as far as I've found, is the (Bible, Koran, TheWord, etc...) of visual studio's T4 Text Templating.

If you already know how to add a T4 template to a project, skip this how to and go to "How to make a T4 template work with a Silverlight project:".

How to add a T4 template to a Visual Studio .Net Project:

Given the following project:

Initial Silverlight Project

Bring up the add new item dialog.

Add New Item

Select a "Text File" and rename the extension to a .tt.

Add a new t4 template

You should end up with a .tt template file and under it where the generated out put of the template will land.

You may also notice the following errors in your errors pane.

  • Compiling transformation: The type or namespace name 'CompilerError' does not exist in the namespace 'System.CodeDom.Compiler' (are you missing an assembly reference?)
  • 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=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
    Compiling transformation: 'System.CodeDom.Compiler.CompilerErrorCollection' does not contain a definition for 'Add'
  • A namespace does not directly contain members such as fields or methods

This is where Oleg Sych comes to the rescue...

How to make a T4 template work with a Silverlight project:

Side Note: So, I had a couple emails back and fourth with Oleg about trying to get T4 to work in Silverlight. Almost all of his emails had some sort of sentence that went like this. "Try adding {SomeSpecialT4RelatedWord} and then do {this special thing} and report your findings back to me". This is where the {SomeSpecialT4RelatedWord} would stump me. So I would search Google for "T4 {SomeSpecialT4RelatedWord}" and Oleg Sych's blog was the first hit for each search. And not only was it the first hit, but when I would look the post for that {SomeSpecialT4RelatedWord} the answer was there and exactly what I needed. Thanks Oleg.


Fortunately, to get the T4 templates to work within a Silverlight project you only need to put an <#@ assembly #> directive telling the template where to get a reference to the System.dll.

Below is a sample template:

<#@ template language="C#" #>
<#@ assembly name="C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll" #>
namespace SampleT4
public class SomeClassSample

And below is the output:

namespace SampleT4
public class SomeClassSample

That's it!!!


Jason Jarrett
@Bill I'm afraid I don't have enough context to really help you. On top of that I haven't worked with silverlight in a long time :(.

Hope you get it figured out.
Bill Lanza
I know this post is old, but....
I am using VS2010 and Silverlight and am trying to implement T4 templates with inheritence. I create a in a separate assembly and it compiles. However, when I try to inherit from it I get the error:

An invalid base class was specified for the Transformation class. The base class must derive from Microsoft.VisualStudio.TextTemplating.TextTransformation.

Is this not possible in SL?

Could you help me understand why you have to have a reference to the two different assemblies with the same type?
Regarding Aquilax comment, using objects defined in both assemblies. Is there any solution for that problem ?

Regards Swan B
It works as long that you don't use objects defined in both assemblies, for example System.Text.RegularExpressions.Regex, otherwise it throws the exception:
Compiling transformation: The type 'System.Text.RegularExpressions.Regex' exists in both 'c:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll' and 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v3.0\system.dll'
Thanks for publishing this knowledge. Saved me a lot of time.
Thank you.
Nice! Thx man.

Best regards,
Alexey Zakharov