T4 Templates - Code Generation in Visual Studio
- By: ben_hoffman
- On: 06/18/2008 12:17:31
- In: Software Development
- Comments: 0
Recently I was listening to the .NET Rocks podcast on my afternoon commute and they were discussing a topic that is somewhat familiar to me, albeit in a different domain, code generation. This time the domain was .NET development, namely code generation within Visual Studio. They were taling about T4 templates, now included in Visual Studio 2008, and I believe they mentioned the editor from Clarius Consulting. It seemed pretty interesting at the time (in comparison to the commute traffic?). While googling the topic later at home, I stumbled upon an article by Hilton Giesenow at his blog where he mentioned that he put together a screencast demonstrating the topic. I really like the screencast medium, and am always interested in checking out one on an interesting topic so I pulled it down and watched it. Pretty neat stuff. Anyway, I filed it away as 'might-be-useful-someday', and got on with my life.
Well, recently I had occasion for about 100+ dependency properties on a class. (I don't want emails saying 'Oh, why would you ever need that, derive from THIS instead' or 'Just use a collection' or something, thats not the point here.) So, dependency properties are exceptionally tedious to add to your class, often times requiring 30ish line of code each - most of it boilerplate. Now, once you have them it gives you tremendous power, but its a bear getting there. Anyway, since there would be, and generally is, tons of boilerplate code here, I figured it would be a good opportunity to test out the code generation stuff thats built in to VS2008. The amazing thing is that you can write C# that writes your C# (wish there was C# intellisense in the template editor). This is very cool and gets you thinking at a different level about your code. Anyway, very cool stuff - you never know what you will find in your travels and when it might come in handy. Learn on!
Comments
There have been no comments made on this article. Why not be the first and add your own comment using the form below.
Leave a comment
Please complete the form below to submit a comment on this article. A valid email address is required to submit a comment though it will not be displayed on the site.
HTML has been disabled but if you wish to add any hyperlinks or text formatting you can use any of the following codes: [B]bold text[/B], [I]italic text[/I], [U]underlined text[/U], [S]
strike through text[/S], [URL]http://www.yourlink.com[/URL], [URL=http//www.yourlink.com]your text[/URL]