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

  • Dad’s Army Too

    For anyone who missed it, the trailer for the forthcoming Dad’s Army movie was released a couple of weeks ago. It's due in cinemas in February.…

  • The Cavorite Sphere

    It suddenly occurred to me that a Cavorite Sphere – as developed by Mr. Cavor in H.G. Wells’ The First Men in the Moon – is something that I did…

  • Belle

    Good movie, go see it. There are no surprises in Belle, the historical outcome is pretty much as you expect it will be; Lord Chief Justice…

  • 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.