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

Using types in a T4 template that exist in the same project as the template

I'm working on my first T4 code generation tool to add some Stored Procedure helper code to my project. I've created custom types (e.g. StoredProcedure and StoredProcedureParameter to help with my code generation and have included the assembly and…
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
21
votes
3 answers

How to reference .Net standard in T4 file?

I have a .Net standard 2.0 library. In this library I have a T4 file. The file contains these rows. <# foreach (MessageType enumValue in Enum.GetValues(typeof(MessageType))) { var name =…
Erik Z
  • 4,660
  • 6
  • 47
  • 74
20
votes
4 answers

How can I use latest features of C# v6 in T4 templates?

I'm trying to run a new T4 template in Visual Studio 2015. However it fails to compile at this line: var message = $"Linked table '{linkedTable}' does not exist."; The compiler reports that the '$' character is unexpected. This syntax should…
Crono
  • 10,211
  • 6
  • 43
  • 75
19
votes
2 answers

T4 template to Generate Enums

I'm looking at creating a T4 template to generate enums of my database. Essentially, I want the same feature as SubSonic e.g. Product.Columns.ProductId for Linq-to-SQL or Entity Framework 4. Any help would be much appreciated. thanks.
Nickz
  • 1,880
  • 17
  • 35
19
votes
4 answers

Setting a breakpoint in a T4 template

I'm trying to debug the execution of a T4 template in Visual Studio 2008. All the information I'm finding on debugging T4 templates in Visual Studio 2008 say that you can set a breakpoint (red dot) in the template as if it were a regular code file. …
Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
18
votes
1 answer

Where is CodeGenerationTools

In entity framework T4 templates, I can use class CodeGenerationTools. For example: void WriteProperty(**CodeGenerationTools** code, EdmProperty edmProperty) { WriteProperty(Accessibility.ForProperty(edmProperty), …
user380719
  • 9,663
  • 15
  • 54
  • 89
18
votes
2 answers

Running a T4 template using C#

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…
Novice
  • 2,447
  • 3
  • 27
  • 33
18
votes
4 answers

TextTemplating target in a .Net Core project

I have recently migrated a test project to .NET Core 2.0. That test project used text templates to generate some repetitive code. The previous project had a build target to generate all T4-templates before build. Therefore, the generated code is…
Georg
  • 5,626
  • 1
  • 23
  • 44
18
votes
6 answers

How to stop T4 from executing every time I switch to another tab?

When I edit T4, the script is executed every time I switch to another file. It is OK for quick simple scripts, but some scripts take long time to execute. Is there a way to disable this behavior? I want the script to run only when I save T4 file or…
Athari
  • 33,702
  • 16
  • 105
  • 146
18
votes
3 answers

What is the difference between <# and <#+ in T4?

What is the difference between the <# tag and the <#+ tag in T4?
Earlz
  • 62,085
  • 98
  • 303
  • 499
18
votes
1 answer

T4 in item template and C# generation

I want to have a T4 template in a Visual Studio item template, but I don't want the "tt" file to appear in the project after the item has been added. In short, I want to generate a .cs file and then discard the .tt file completely. The code…
LBugnion
  • 6,672
  • 2
  • 24
  • 28
18
votes
2 answers

Wrong indentation with t4 templates

I'm currently working with T4 templates and I have noticed that sometimes the code is not indented properly, how can I avoid that? For instance I have this code in the template } <# } #> this.cmbDecisionList.Dat = dataSource; …
John Jerrby
  • 1,683
  • 7
  • 31
  • 68
18
votes
3 answers

How do I pass a string parameter to a t4 template

Hi I am trying to find a way to pass a normal string as a parameter to a text template. This is my Template code, if someone could tell me what I would need to write in c# to pass my parameters and create the class file. That would be very helpful,…
Toby Jackson
  • 183
  • 1
  • 1
  • 4
18
votes
1 answer

Reflection with T4 get assemblies

I want to get all of class in the specific assembly this is my code var assembly=Assembly.GetExecutingAssembly(); var assemblies = assembly.GetTypes().Where(t => String.Equals(t.Namespace, "RepoLib.Rts.Web.Plugins.Profiler.Models",…
user1968030
18
votes
2 answers

Product of build-time T4 transformation is used only in the next build

I have a VS project that contains: a pre-build action running TextTransform on a template.tt to generate generated.cs generated.cs listed as one of the files to compile (i.e. in the list of project files) When I build the project, the pre-build…
Zvika