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
12
votes
3 answers

how to disable T4 template auto-run in visual studio (2012)?

I have some T4 templates in my project. Whenever I make changes and save the tt file, it auto update the generated files. This is a template that loops all tables in a database and generates about 100+ files. So visual studio hangs for a few seconds…
Calvin
  • 1,153
  • 2
  • 14
  • 25
11
votes
2 answers

Cannot debug T4 template in VS2017

In VS2017 Community, I cannot debug T4 Templates, which works in 2015. I have a very basic template, such as this... <#@ template debug="true" hostspecific="false" language="C#" #> <#@ output extension=".txt" #> <# var a = "Hello"; var b =…
Raheel
  • 421
  • 1
  • 4
  • 7
11
votes
2 answers

T4 templates not generating output in new VS2017 csproj projects

I migrated a project.json/.xproj project to the newer CS2017 .csproj format. The project contains a T4 (.tt) template file. It doesn't regenerate its output on save or build. The output .cs file isn't nested below the .tt file either. Is there…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
11
votes
3 answers

Running T4 templates from other T4 template

does anyone know if it's possible to run T4 template file from another T4 template, inside VS2010 Thank
muek
  • 1,080
  • 2
  • 16
  • 36
11
votes
3 answers

Automatic INotifyPropertyChanged Implementation through T4 code generation?

I'm currently working on setting up a new project of mine and was wondering how I could achieve that my ViewModel classes do have INotifyPropertyChanged support while not having to handcode all the properties myself. I looked into AOP frameworks but…
chrischu
  • 3,047
  • 3
  • 27
  • 44
11
votes
1 answer

Get all methods that are decorated with a specific attribute using T4/EnvDTE

I'd like to get a list of all public methods in my project that are decorated using MyAttribute using T4/EnvDTE. I know this can be done with reflection, but I don't want to load the assembly and reflect over it in a T4 template, rather, I want to…
Omar
  • 39,496
  • 45
  • 145
  • 213
11
votes
1 answer

Compiling dynamic code at runtime using T4 and C#

The articles I have read on T4 using TextTemplatingFilePreprocessor show how to dynamically generate code that becomes part of a project, and is compiled with the project. Is it possible to use T4 to generate code that is compiled at runtime,…
IamIC
  • 17,747
  • 20
  • 91
  • 154
11
votes
3 answers

Design Time Reflection

Is there a way to do reflection pre-compile - at design time? My intent is to use T4 to spit out custom codes based on classes that implement certain interfaces. I know I can call upon reflection, but I want the T4 script to spit out the additional…
Alwyn
  • 8,079
  • 12
  • 59
  • 107
11
votes
3 answers

Finding a property is Primary Key in POCO Template t4 generator

I'm using the POCO t4 template generator that comes with VS 2012. I made few changes to include the Entity.Name, but I'm not able to figure out the primary key. public string EntityClassOpening(EntityType entity) { return string.Format( …
wonderful world
  • 10,969
  • 20
  • 97
  • 194
10
votes
1 answer

VS2022 17.2.0 Preview 2.0: T4 template serialization exception when accessing projects, etc

Using VS2022 17.2.0 Preview 2.0 to generate data layer using T4 templates. Part of the T4 uses VS interop / DTE to access projects in solution. The following T4 is a test: <#@ template debug="false" hostspecific="true" language="C#" #> <#@ assembly…
GK2226
  • 103
  • 1
  • 6
10
votes
3 answers

T4 template assembly directive

I have a custom dll, which has a class in it, and (for simplicity's sake) there's a method on it which'll return a string. I have a project, which references said dll, and I want to use a (not preprocessed) T4 template in that project, which calls…
TDaver
  • 7,164
  • 5
  • 47
  • 94
10
votes
3 answers

EF4: Get the linked column names from NavigationProperty of an EDMX

I am generating POCOs (lets say they are subclasses of MyEntityObject) by using a T4 template from an EDMX file. I have 3 entities, e.g.: MyTable1 (PrimaryKey: MyTable1ID) MyTable2 (PrimaryKey: MyTable2ID) MyTable3 (PrimaryKey: MyTable3ID) These…
user432219
10
votes
15 answers

Code Generators or T4 Templates, are they really evil?

I have heard people state that Code Generators and T4 templates should not be used. The logic behind that is that if you are generating code with a generator then there is a better more efficient way to build the code through generics and…
jwendl
  • 942
  • 7
  • 13
10
votes
2 answers

T4 for Sharp Architecture/Northwind Problem

I have just downloaded sharparchitecture/Northwind and i'm trying to get crud scaffolding to work. I have changed nothing except adding missing reference to this class library. I try to run ScaffoldingGeneratorCommand.tt and I hit following 3…
PK.
  • 588
  • 1
  • 4
  • 12
10
votes
1 answer

T4MVC is generating T4MVC.cs and T4MVC1.cs

VS2017 15.4.1 ASP.NET MVC 5.2.3 T4MVC 4.0.0 AutoT4MVC 1.5.3 Resharper I have been using T4MVC] for many months in this project without issue. However today each time I change a controller method it is generating two T4MVC files: instead of…
TheEdge
  • 9,291
  • 15
  • 67
  • 135