Introducing T4 Text Template Support In Rider 2019.3
Có thể bạn quan tâm
Matthias Koch 
Brace yourself! Another highly requested feature request was finally resolved – Support for T4 Text Templates. For those folks who don’t know what it is: T4 stands for Text Template Transformation Toolkit – a mixture of code blocks and text that can generate new text files or even code (similar to Razor). T4 templates are very handy to generate output based on our project model or external databases. For instance, using linq2db we can easily generate POCOs based on a database schema to allow for fast and type-safe database accessing.
Many ReSharper users might be familiar with the ForTea plugin that was developed and maintained by Julien Lebosquain. A huge round of applause for his amazing work and permission to integrate it directly in Rider! Besides some minor adjustments, the most significant missing part was the generation itself. Did we mention it’s still open-source?!
How does Rider help with T4 Text Templates?
Let’s start small by adding a new T4 text template file. There are regular .tt template files on which the generator is executed, and .ttinclude files, which allow for code sharing:

Also a good reminder, that we can just type in the New File dialog and search for the file type we want.
As mentioned, the regular .tt template files are meant for generation. So here we are with some actions – Execute Template and Debug Template for design-time templates, and Preprocess Template for our run-time templates:

As you may have guessed, debugging also works fluently. Just place a breakpoint, hit Debug Template, and we can use all the debugger goodies, like seeing stack traces, inspecting variables, modifying the execution pointer, and many more:

For standard control blocks, the code completion integration will let us choose from a list of available keywords:

For navigating around, we can either use the normal symbol navigation (a.k.a. GoToFileMember) or make use of the Structure tool window:

Good old code inspections & quick-fixes can help us to see errors at a glance and fix them right away. For instance here, when a feature block was misplaced before a statement block:

And of course, it wouldn’t be Rider, if there wasn’t integration with all the other C# features that we’re familiar with, like code completion, namespace imports, live and postfix templates, formatting for control blocks, and last but not least, symbol navigation:

Pro tip on the side: as shown in the GIF above, regular strings will turn into interpolated strings if we try to use them as such. Thanks to code completion!
We’re still working on shipping additional features around T4 templates, until then, download Rider 2019.3, try it out and tell us what you think!
code analysis and quick-fixes code completion generation and templates Rider- Share
Subscribe to a monthly digest curated from the .NET Tools blog:
Subscribe form By submitting this form, I agree to the JetBrains Privacy Policy Notification iconBy submitting this form, I agree that JetBrains s.r.o. ("JetBrains") may use my name, email address, and location data to send me newsletters, including commercial communications, and to process my personal data for this purpose. I agree that JetBrains may process said data using third-party services for this purpose in accordance with the JetBrains Privacy Policy. I understand that I can revoke this consent at any time in my profile. In addition, an unsubscribe link is included in each email.
SubmitThanks, we've got you!
Từ khóa » How To Use T4 Templates In Visual Studio 2019
-
Design-Time Code Generation By Using T4 Text Templates
-
Code Generation And T4 Text Templates - Visual Studio (Windows)
-
C# Code Generation Using T4 Templates - YouTube
-
T4 Beginner Tutorial: Generate C# Classes Based On Text Definitions
-
Code Generation/scaffolding With Visual Studio T4 Templates - Tritac
-
How To Install T4 Toolbox For Visual Studio 2019 - Ashley Hollis
-
Executing A T4 Text Template In Visual Studio Code - Stack Overflow
-
T4 Support - Visual Studio Marketplace
-
T4 Template Not Showing In Solution - Visual Studio Feedback
-
T4 Editor - VS Add-in For Editing T4 Templates - Devart
-
T4 Templates And Visual Studio Item Templates - ADocLib
-
How To Auto-Run T4 Templates In Visual Studio Builds
-
T4 Templates - Pluralsight
-
T4 Templates Updated For Visual Studio 2019 - P3.NET