We recommend using Visual Studio 2017

What's New in Code Generation and Text Templates

In Visual Studio 2012, there are several enhancements to text templates. .

  • Transform All Templates has moved. You’ll now find this control on the Build menu and the build toolbar, and not on the Solution Explorer toolbar.

    As before, transformation also occurs whenever you save or move the focus away from a template, and when you build your project.

  • Debug design-time text templates. You can set breakpoints in a design-time text template and run it in the debugger. Right-click the text template in Solution Explorer, and choose Debug Text Template.

    As before, you can also set breakpoints in a runtime template, and step through the template when you debug the application of which it is part.

    For more information, see Debugging a T4 Text Template.

  • Public or internal visibility for the code generated by runtime text templates. Use the visibility attribute in the template directive.

    See T4 Template Directive.

  • Turn off line pragmas in runtime templates to avoid source control merge issues in the generated code. Set linePragmas="false" in the template directive.

    See T4 Template Directive.

  • More reliable access to Visual Studio services. If you write a host-specific design-time template, use this.Host.GetCOMService(serviceType) to access services such as DTE.

  • CompositionHost.Initialize() creates a MEF container in the app domain, from which you can access MEF components of Visual Studio.

  • Derived host-specific templates. You can write a template that acts as a base from which others can be inherited, and in which hostspecific="true" is specified in the template directive. In the derived templates, write hostspecific="trueFromBase" to avoid defining this.Host twice.