Visual Studio 2010
Code Generation and Text Templates

In Visual Studio, a text template is a mixture of text blocks and control logic that can generate a text file. The control logic is written as fragments of program code in Visual C# or Visual Basic. The generated file can be text of any kind, such as a Web page, or a resource file, or program source code in any language. Text templates can be used at run time to produce part of the output of an application. They can also be used for code generation, in which the templates help build part of the source code of an application.

Run-time text generation lets you use templates to define part of the output of your application. For example, you could use a template to define an HTML report. The template looks like the generated output, but contains fragments of program code at the points where the output can vary. Whenever you edit the template during development, the template is transformed to a class. This class is compiled as part of the application. The similarity of the template to the resulting output helps you avoid mistakes when you need to change it.

Code generation lets you use templates to generate part of your application's source code and other resources from a model. For example, your model could be a diagram that defines the flow of control through a series of user dialogs. Whenever you edit the model during development, the text templates generate part of the application code and configuration files from the diagram. When the users' needs change, it is easy to discuss and change the diagram because it focuses on the business requirement.

NoteNote

A model is a file that one or more text templates read. It can be any kind of file or database. It does not have to be a UML model or a Domain-Specific Language model. A benefit of using text templates is that you can design the model to focus on an important aspect of your application that you want to make easy to change.

In This Section

Generating Text Files at Run Time by Using Preprocessed Text Templates

In any application that generates text files, precompiled text templates are an easy and reliable method of defining the text. However, this method cannot be used for text templates that change at run time.

Code Generation in a Visual Studio Project

Generating code and other resources from a model allows you to update your application by updating the model.

Code Generation in a Build Process

If you have installed Visual Studio Visualization and Modeling SDK, you can ensure the generated software keeps up to date with changes in the model.

Writing a Text Template

The syntax of a text template file.

Walkthrough: Generating Code by using Text Templates

A demonstration of one way to use code generation.

Troubleshooting Text Templates

How to debug text templates, and some common text template errors.

Generating Files with the TextTransform Utility

The command-line tool that you can use to run text template transformations.

Customizing Text Transformation

How to write directive processors and custom templating hosts for your own data sources.

Text Template Glossary

Key terms related to text templates.

See Also

Concepts

Other Resources

Page view tracker