There are two types of T4 templates, Run-time and Design-time:
- Run-time templates help defining templates which you want to run at run-time in your application to produce text output. For example, generating an email, an HTML report page, etc. So they are usually used for run-time text generation. These templates use
TextTemplatingFilePreprocessor
as their custom tool. More info: Run-Time Text Generation with T4 Text Templates - Design-time templates help defining source code and other resources of your application. For example to generate model classes based on metadata or database, or generate resources, etc. So they are usually used for design-time code generation. These templates use
TextTemplatingFileGenerator
as their custom tool. More info: Design-Time Code Generation by using T4 Text Templates
Before choosing a template type, you should answer the following questions:
- Do you want to to generate text-output at runtime, for example to send an email, to show a message to the user, and etc.? If the answer is yes, you need to use Runtime Text templates.
- Do you want to generate a resource at design time as part of the Project, for example generate a C# class, a ResX resource, or etc. which will be built as part of the project? If the answer is yes, then you need Design-time Text Templates.
Example 1 - WinForms Run-time T4
Follow these steps:
- Create a new WinForms project with the following name:
WindowsApplication1
- Add a new item and select Runtime Text template with the following name:
RuntimeTextTemplate1
- Add the following content to the file:
<#@ template language="C#" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> Items: <# for (int i = 1; i <= 10; i++){ #> Item <#= i #> <# } #>
- Save the file and if VS asked a question, let the code generator run the code and tell it not to ask the question again.
- Drop a button with
button1
as name, on Form1 and double click on it to generate the click event handler and change the code like this:private void button1_Click(object sender, EventArgs e) { var template = new RuntimeTextTemplate1(); var content = template.TransformText(); MessageBox.Show(content); }
- Run the application, click on the button and see the generated text as run-time.
Example 2 - WinForms Design-time T4
Follow these steps:
- Create a new WinForms project with the following name:
WindowsApplication1
- Add a new item and select Text Template with the following name:
TextTemplate1
- Add the following content to the file:
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ output extension=".cs" #> using System; using System.Windows.Forms; <# var entities = new string[]{"Product", "Customer"}; foreach (var entity in entities) {#> public class <#=entity#>Form: Form { public <#=entity#>Form(): base() { this.Text = "<#=entity#>"; } } <#}#>
- Save the file and if VS asked a question, let the code generator run the code and tell it not to ask the question again.
- Drop a button with
button1
as name, on Form1 and double click on it to generate the click event handler and change the code like this:private void button1_Click(object sender, EventArgs e) { var p = new ProductForm(); var c = new CustomerForm(); p.Show(); c.Show(); }
- Run the application, click on the button and see the generated text as run-time.