Questions tagged [t4]

T4 stands for Text Template Transformation Toolkit and is Microsoft's free and open-source template-based text generation framework included with Visual Studio.

T4 is used by developers to automate repetitive text file creation processes. Text files can be anything from HTML code, XML, XAML and most often compilable code files (Visual Basic, C# or other languages).

T4 uses a custom template format which can contain C# or Visual Basic executable code along with string literals that become part of the generated file.

Advantages

  1. Visual Studio integration

    Whenever a developer creates and saves a file with extension *.tt, Visual studio automatically runs the T4 templating engine and processes the saved template. There's no need to run any external tool which makes it transparent for developers to use.

  2. .NET language support

    It's easier for developers to use something that they know rather than reinventing the wheel. Developing T4 templates is very similar to writing ASPX files where executable code sits along string literals (HTML code interpreted by the client browser) and manipulates it as required.

Disadvantages

  1. Lack of Visual Studio tooling support

    It's possible to write and save T4 templates inside Visual Studio but developers won't have any other advantages of a modern development environment like code formatting/colouring or code IntelliSense. Third-party tools are required to provide this functionality. Available extensions include Tangible T4 Editor and Devart T4 Editor among others.

  2. One template, one output file

    It's sometimes desired for a template to process several resources (either files, database tables, etc.) at once and output just as many text files. By default, T4 doesn't support multiple file output. But there are solutions to overcome this limitation as well.

Usage

There are lots of different usage scenarios for T4 templates that can greatly benefit developers to shorten their development time. There are templates on the internet for:

Microsoft also uses T4 templates for ASP.NET MVC controller and view generation when developers add a new Controller or View from within Visual Studio.

1602 questions
17
votes
2 answers

How to generate all my entities composed two tables for each entity via a T4 automation

I have a class library project for a data access layer that uses Entity Framework 4. My project needs a versioning concept. My database contains many tables that contain «Id» and «CreationDateTime». Each table is associated with a version table…
ylegare
  • 171
  • 3
17
votes
4 answers

MSBuild support for T4 templates in Visual Studio 2017 RTM

In Visual Studio 2015, I'm using the NuGet package Unofficial.Microsoft.VisualStudio.TextTemplating.14.0.0 which allows me to transform T4 templates directly from MSBuild, whenever a project is built. In Visual Studio 2017 RTM however, this breaks…
Sam
  • 6,167
  • 30
  • 39
17
votes
6 answers

Get referenced project's path in T4 template?

I have a solution that has a few projects in it. I'd like to create some T4 templates in one of my test projects to generate tests based on code in another project. The test project has a Project Reference to the other project. The problem I have is…
Jaxidian
  • 13,081
  • 8
  • 83
  • 125
17
votes
1 answer

Visual Studio T4 vs CodeSmith

I've been using CodeSmith for the past 2 years and love what it does for me. However, I also know about T4 which is built in to Visual Studio and can do some pretty cool stuff too. Based on conversations with friends T4 in VS2010 T4 is going to be…
Jake
  • 906
  • 10
  • 23
17
votes
2 answers

Can I use T4 programmatically from C#?

I am writing software that produces C# code. Mostly I am using StringTemplate and StringBuilder. Is there any way to use T4 templates direct from my code?
Đorđe
  • 447
  • 5
  • 18
17
votes
4 answers

T4 template will not transform with build

I'm using VisualStudio Ultimate 2012 (Update 1) on Windows 7. I can get my T4 template to generate a file by: right click [tt file] | run Custom tool. That works great! (yay for me). It also works if I run the menu command: Build | Transform All T4…
Lorne Redmond
  • 263
  • 1
  • 3
  • 13
17
votes
1 answer

T4 code generation: access types in current project

Using T4 code generation, is it possible to access the types defined in the current project? For example, if I have an interface and I want to delegate its implementation to another class, i.e. interface IDoSomething { public void…
Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
17
votes
1 answer

Error "A template containing a class feature must end with a class feature"

I was developing a C# T4 pre-processed template, under Visual Studio 2010, when I have got the following compilaton error: A template containing a class feature must end with a class feature
Julio Nobre
  • 4,196
  • 3
  • 46
  • 49
17
votes
2 answers

MvcTextTemplateHost not found

I'm trying to use the T4 templates in my MVC project. This screencast suggests just copying the existing T4 templates for MVC into your solution and going from there. However when I try to compile I get this error: Error 1 Compiling…
Kirschstein
  • 14,570
  • 14
  • 61
  • 79
16
votes
3 answers

Net Framework 4.8 EF6 EDMX T4 Error _generationEnvironment is NULL

This started with an EDMX suddenly not generating class files. The .context.cs file contains the class declaration like public virtual DbSet myTable { get; set; } but the actual class files are not being generated. Both .tt files are…
Serexx
  • 1,232
  • 1
  • 15
  • 32
16
votes
4 answers

What is the '.tt' extension?

I work with a bunch of something.js.tt JavaScript files using Knockout and a bunch of something-else.tt HTML files. The infrastructure is mostly a C backend with Perl serving API and we use these .tt files to show the HTML and .js.tt to serve the…
TheWebs
  • 12,470
  • 30
  • 107
  • 211
16
votes
5 answers

Debugging T4 Template in VS 2010 Crashes IDE

I'm trying to debug a slightly-modified version of the ADO.NET POCO Entity Generator template using the directions Oleg Sych published a few years back. I modified the DbgJITDebugLaunchSetting key as recommended. I get a dialog indicating that a…
Eric J.
  • 147,927
  • 63
  • 340
  • 553
16
votes
9 answers

Metadata file not found - Data.Entity.Model

Anyone having similar problem, while creating webservices? Compiling transformation: Metadata file 'D:\Program Files\VS2013\Common7\Tools..\IDE\Microsoft.Data.Entity.Design.dll' could not be found D:\PROJEKTY\apki\ws2\WS\WS\DataModel.tt I…
16
votes
3 answers

EnvDTE not found in VS2012 works in VS2010

I'm using EnvDTE to do some code generation within my T4 Templates. I have the code working correctly in Visual Studio 2010, however I've just started using Visual Studio 2012 and now when I try to run my templates I get the following…
Midimatt
  • 1,114
  • 2
  • 17
  • 35
15
votes
3 answers

VS2022 Extensibility: How to resolve "The type 'XXX' exists in both 'Assembly 1' and 'Assembly2' in T4 template execution

I have an extension I'm updating from VS2019 to VS2022. It's a DSL using the Modeling SDK and has code generation via T4 templates. I have it pretty much converted but, when running the T4s, I get Compiling transformation: The type 'SourceControl'…