qrjo
qrjo

Reputation: 201

Using .NET Core assemblies in T4 templates

We're currently migrating a project from .NET Framework to .NET Core 3.1 and we have a problem getting some of our T4 templates to work. These templates import one or more assemblies from our solution in order to generate code using reflection. The assemblies are .NET Core 3.1 libraries. The issue seems to be that the T4 engine in Visual Studio 2019 is running on Framework and is unable to load the Core assemblies properly.

When running the templates, I'm getting a ReflectionTypeLoadException. After inspection of the exception, it becomes clear that the T4 engine can't find System.Runtime. There is some workaround for that (here), but when I apply that, that just moves the problem to the next dependency, which is a NuGet package. So the workaround doesn't work.

Is there any way to make T4 templates work with .NET Core 3.1 assemblies? Or is there any similar alternative to T4 templates that works with .NET Core 3.1?

Upvotes: 5

Views: 6519

Answers (1)

RdJNL
RdJNL

Reputation: 334

I had this issue and couldn't find any good solutions. So I developed an alternative T4 tool that uses a hybrid of Visual Studio's .NET Framework T4 capabilities for preprocessing and a .NET Core executable to execute the template code. It is available from GitHub. It works well for my project and hopefully it can help some others too.

Upvotes: 8

Related Questions