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!!!

6 comments:
Nice! Thx man.
Best regards,
Alexey Zakharov
Blog: http://weblogs.asp.net/alexeyzakharov/
Thank you.
Thanks for publishing this knowledge. Saved me a lot of time.
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'
Regarding Aquilax comment, using objects defined in both assemblies. Is there any solution for that problem ?
Regards Swan B
Could you help me understand why you have to have a reference to the two different assemblies with the same type?
Post a Comment