Using T4 text templates in the software development process is a common task. Visual Studio supports design-time code generation and run-time text generation by using preprocessed T4 text templates. However, there’s a third use case which is not supported by Visual Studio: Run-time text generation by using T4 text templates.
Design-Time Code Generation
If you need to generate code or other textual files within your Visual Studio project, you typically write a template that can be used to generate code or text from a model. If the model changes, you just need to regenerate the code.
The design-time code generation is well integrated into the Visual Studio. For more Information see the MSDN article about design-time code generation.
Run-time Text Generation by using Preprocessed T4 Text Templates
To generate text output at run-time, you can add a preprocessed text template to your Visual Studio project. Visual Studio automatically generates a code-behind file for the template. The generated code file contains the source code for a text generator class that can be used from your application by calling the TransformText method. Whenever you change the template, Visual Studio regenerates the subsidiary code file. For more Information see the MSDN article about run-time text generation.
There is a drawback by using preprocessed T4 text templates. The code for the text generator will be created at design-time. At run-time you can only pass data to the generator and the text output has a fix structure. The user cannot customize the template to his own needs.
Run-time Text Generation by using T4 Text Templates
In applications where you need customizable T4 text templates or where the user shall be able to create its own templates, you cannot use preprocessed T4 text templates. In more complex scenarios you may even have a meta template that can be used to generate different T4 text templates. There are many other use cases where you need more flexibility than given with the Visual Studio T4 text generation.
The OMS.Ice – T4 Text Template Generator is all in one, a T4 text template parser, compiler and generator. At runtime, the generator parses a T4 text template, builds code in the selected language (C# or VB), compiles that code into memory, and finally executes the text generator.