T4 text templating in .NET Core and above

David Reis 1 Reputation point
2021-10-01T13:06:23.25+00:00

After some testing, it looks like the TextTemplating library that is part of VisualStudio (Microsoft.VisualStudio.TextTemplating.15.0.dll) can't be used in the newer "dotnet" command line toolchain as it depends on the WPF assembly WindowsBase.dll. This causes the .tt code generation to fail, even on a Windows machine.

After some investigation, we found some open-source projects that are able to do .tt code generation, most notably mono/t4, but these solutions have the disadvantage of not being so well integrated with VisualStudio as the current TextTemplating is. For example, it's harder (if not impossible) to get design-time generation and we also lose the ability to trigger the code generation from the context menu in the solution explorer.

Given these drawbacks, I'd just like to know what is Microsoft's position on this. Is there a plan to update the existing TextTemplating libraries so that they are compatible with the "dotnet" toolchain while still maintaining good integration within the VS IDE? Or is TextTemplating being left behind and we must rely on the not-so-well-integrated open-source solutions?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,002 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,226 Reputation points
    2021-10-01T14:59:05.887+00:00

    See this thread

    https://developercommunity.visualstudio.com/t/build-transform-all-t4-templates-not-working-for-d/446832

    If can add t4 templates as a build step in a .net core project, but the step is only called on build, not run

    https://github.com/nogic1008/T4Sample


  2. Paul Michalik 1 Reputation point
    2022-08-25T21:17:03.89+00:00

    Obviously, a lot of people ran into these incompatibility issues and started their own ports of T4 tooling, as a quick search on GitHub demonstrates. This is a pity, since with every variation of the toolchain we seem to be losing a feature rather than the opposite... I vote for Microsoft taking over again and providing a cross platform Visual Studio independent solution, which does work in Visual studio, including debugging and Intellisense support.

    I have set up a "universal" tooling which works in Visual Studio and as a command line tool on all supported platforms but it was a lot of work and unnecessary frustration.


  3. Jonathan Martens 0 Reputation points
    2024-10-02T05:05:46.99+00:00

    In the mean time Microsoft seems to at least addresed this problem to some extent with the creation of TextTranformCore.exe as per https://devblogs.microsoft.com/dotnet/t4-command-line-tool-for-dotnet/.

    They even suggest a PreBuild step based solution for build time generation (with some limitations though)

    0 comments No comments

  4. Karen Payne MVP 35,436 Reputation points
    2024-10-03T10:58:58.6833333+00:00

    I have never had any issues with text templating and is still supported.

    Seems Microsoft is pushing source generators over text templating.

    Example for text templating using NET8.

    Also, see T4 language extension for syntax coloring.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.