Gavin Greig (ggreig) wrote,
Gavin Greig

Visual Studio's Best Kept Secret: T4

Beats me why Microsoft haven't made more of a fuss about this. It's obvious, if you use it, that Visual Studio contains a code generation system, but what's much less obvious is that there's support for writing your own code generation templates within Visual Studio - straight out of the box.

It's called Text Template Transformation Toolkit, or T4, it's been there since Visual Studio 2005, and you can code in it, using C# or Visual Basic, to produce virtually any text-based output you choose.  To see it in action, just create a new text file in your project, and give it the extension "tt". Underneath it, another file will appear that's the code generated by the template. It's regenerated whenever you save the template.

For a brief introduction, watch the first 20 minutes of Advanced code generation patterns with T4 & DSL Tools. The speaker, Oleg Sych, has a blog that's an indispensable reference. For an overview, including links to key topics in Oleg's blog, try Scott Hanselman. If you're serious about giving it a try, you will probably want to get the T4 Editor from Clarius. There's a freeware Community Edition and a paid-for Professional Edition.

Tags: software development

  • A March for Independence (in April)

    On Saturday, YES North East Fife organised a march across the Tay Bridge from Fife to Dundee followed by a rally in the City Square. It was…

  • State of the Union

    Two and a half years ago, almost to the day, I stopped blogging on political matters as the argument I cared about was over for the time. My last…

  • World Porridge Day

    Today is World Porridge Day, which is being promoted by Mary’s Meals. Mary’s Meals provide a daily meal of maize porridge ( likuni phala) to some…

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.