Running a T4 template using C#

You can easily achieve it, when you using VS2010. If you add a new file to the project, choose a preprocessed text template file. You can edit the template just as normal. Instead of generating the output directly, the file generates the code that is generated normally. I know it sounds confusing. But what you see in your output file is the code generated by the text templating toolkit to get your output (more or less).

This is a short example of a preprocessed text template named "TestTemplate.tt" and how do you use it in your code:

The tt-file:

<#@ template language="C#" #>
Some output.

Code:

using System;
using System.Diagnostics;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            TestTemplate testTemplate = new TestTemplate();
            Debug.Print(testTemplate.TransformText());
        }
    }
}

I'd recommend the preprocessed route as answered above by @jb_.

As an alternative, if you need your templates to still be editable without a compile step for use with your custom C# application, and the application will only be deployed on machines alongside Visual Studio, you can write a custom host.

http://msdn.microsoft.com/en-us/library/bb126519.aspx


Comments

  1. Leon

    • 2019/3/19

    You can easily achieve it, when you using VS2010. If you add a new file to the project, choose a preprocessed text template file.

  2. Giuliani

    • 2019/6/7

    I have T4 template (mycode.tt) which generates a cs file. I usually right click the tt file and select RunCustomTool which internally takes an xml file and generate code for me. Now i want to run the custom tool using a c# windows application. So onclick of a button i want to run the Custom Tool . Is it possible to run the CustomTool from c#. Edit:

  3. Fabian

    • 2017/3/10

    In Solution Explorer, on the shortcut menu of your project, choose Add > New Item. · In the Add New Item dialog box, select Runtime Text Template 

  4. Musa

    • 2021/7/21

    In Visual Studio 2015 Update 2 and later, you can use C# version 6.0 features in T4 templates directives. 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. There are two kinds of T4 text templates: run time and design time. Run time T4 text templates

  5. Chance

    • 2021/2/27

    In this article. Run time T4 text templates; Design time T4 text templates; See also. In Visual Studio, a T4 text template is a mixture of 

  6. Mazza

    • 2019/1/20

    Learn how to generate C# code during design time using T4 text templates in Visual Studio.Get T4 Toolbox: https://marketplace.visualstudio.com/items?itemName

  7. Ismael

    • 2015/6/10

    Really powerful tool! T4 came to my mind because I answered a question at StackOverflow 4 days ago: Is T4 template run every time it is 

  8. Joel

    • 2018/10/9

    Generating your first code. Open up a command prompt (go to the windows menu and just type cmd.exe). Next navigate to your new folder (cd c:\templatetest). Now navigate to the folder on your desktop and create a new file in that folder called “test.tt”. If you used the right click mouse button menu to create a text file then you might find

  9. Ayan

    • 2018/7/24

    In 2021 this isn't working at all for me. When running a template, nothing happens. Then I run it again and get «Could not execute T4 file: 

  10. Andre

    • 2017/5/1

    When you want to send emails from .net applications how do you do it? Our code challenge this week explores the process of building .NET email templates usin

  11. Diaz

    • 2018/9/25

    Auto execute T4 template on build; Show pre and post transformation messages and list of files; Select specific or all T4 files for auto 

  12. Harley

    • 2019/10/1

    In Visual C ා or visual basic, a fragment of control logic Execute a design time T4 text template in visual studio to define part of 

  13. Cassius

    • 2016/7/28

    Right now I have implemented (like MSDN suggests) a custom T4 host to generate my T4 results and I use it in this way: const string content = @"c:Simple.tt" 

Comments are closed.

Recent Posts