C#

T4 Template Goodness

I must admit I kinda missed the boat with T4 templates and what was capable of them, which annoys me, so I set out to try and have a look at them the other day, but low and behold Karl Shifflett has beaten me too it.

Karl has an excellent blog post http://karlshifflett.wordpress.com/2009/10/30/t4-preprocessed-text-templates-in-visual-studio-2010/ which talks about the new partial class support available for T4 templates in VS2010.

In case you have totally missed the boat, T4 templates are a text template that when saved will run some code. Now this code can do all sorts of things as demonstrated so beautifully by Daniel Vaughan in his excellent T4 EnvDTE article over at codeproject

http://www.codeproject.com/KB/codegen/T4Metadata.aspx

Daniel basically shows how to use T4 to create type safe metadata to get rid of magic strings (which we all hate).

Ok so getting back to T4 basics, T4 uses some classic ASP type syntax and you can embed c# code between special characters. Again see Karl for an example of this.

One thing is for sure T4 looks to be getting a whole lot better in VS2010. can’t wait.

Oh and here are some other good T4 resources:

T4: Text Template Transformation Toolkit

http://www.olegsych.com/2007/12/text-template-transformation-toolkit/

How to create a simple T4 template

http://www.olegsych.com/2007/12/how-to-create-a-simple-t4-template/

Advertisements