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:
Bring up the add new item dialog.
Select a "Text File" and rename the extension to a .tt.
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=2.0.0.0, 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!!!
Hope you get it figured out.
I am using VS2010 and Silverlight and am trying to implement T4 templates with inheritence. I create a baseclass.tt 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?
Thanks,
Bill
Regards Swan B
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'
Best regards,
Alexey Zakharov
Blog: http://weblogs.asp.net/alexeyzakharov/