Your opinion matters! Please fill in the InfoQ Survey!

Dependency Injection, Debugging added to T4 for Visual Studio 2012

| by Jonathan Allen Follow 254 Followers on Jun 08, 2012. Estimated reading time: 1 minute |

A note to our readers: As per your request we have developed a set of features that allow you to reduce the noise, while not losing sight of anything that is important. Get email and web notifications by choosing the topics you are interested in.

There are quite a few improvements added to Visual Studio’s T4 system for Visual Studio 2012. The highlights include MEF support for deploying directive processors and easier debugging.

Directive processors are used to inject code and feed external data into a T4 template. In the past it could be quite tedious to develop and deploy directive processors. With VS 2012 and MEF it is supposed to become much simpler. To start with one needs to implement the IDirectiveProcessor interface. The details are not yet clear, but supposedly the attributes DirectiveProcessor and SupportedDirective handle the heavy lifting.

Preprocessed Templates have been renamed “Design-time templates”. For those of you who don’t know, a preprocessed template is a T4 template that has been compiled into a normal .NET assembly. Once this is done it loses its dependency on Visual Studio and can thus be called from any other application.

With Visual Studio 2012, developers gain the ability to add custom attributes to the classes that represent the preprocessed templates via directive processors. The specific use case in mind is adding MEF exports to the template class.

You can also change the visibility of the template class using the @template directive.

To address a bad interaction between Visual Studio and COM, the new ServiceProviderExtensions. GetCOMService function should be used instead of the normal GetService function. This is only needed when “using host-specific templates to access VS services”.

The final addition to T4 is easy template debugging. In the past you would need to add a debug directive and Debugger calls. Now you set simply set a breakpoint in your T4 templates, right-click on the file, and immediately start debugging.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread


Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and don't miss out on content that matters to you