Customizing T4 Text Transformation

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2019. Download it here

Text templates are a feature of Visual Studio that allow you to generate program code or other text files through a transformation process. Using Visual Studio SDK, you can extend the default template transformation process by customizing the text template directive processor or the text template host.

In This Section

The Text Template Transformation Process
Describes how text transformation works, and explains the role of the template host and the directive processors.

Creating Custom T4 Text Template Directive Processors
The directive processor deals with directives in your template, such as <#@template#>. It runs during the compilation of the template, and can load assemblies and other resources. It can also insert code that will load resources at runtime. By defining your own directive processor, you can reduce the complexity of your templates.

Invoking Text Transformation in a VS Extension
If you are writing a Visual Studio Extension such as a menu command or event handler, your extension can use the Text Templating Service to transform any text template. You can pass parameter data into the template by using the Session object, and get the values from within the template by using the <#@parameter#> directive.

Processing Text Templates by using a Custom Host
When the code of the text template executes, the host provides access to external files and the state of the application. For example, the host that runs text transformations in Visual Studio can provide access to solution explorer. It also displays errors in the error message window. If you want to run text transformations in a different context, you can define your own host that provides access to the services available in that context.

If you are writing a Visual Studio Extension, consider using the existing text transformation service instead of writing your own host. For more information, see Invoking Text Transformation in a VS Extension.

Reference

Writing a T4 Text Template

Provides the syntax of text template directives and control blocks.