简体   繁体   English

在 T4 模板中使用 .NET Core 程序集

[英]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.我们目前正在将一个项目从 .NET Framework 迁移到 .NET Core 3.1,但在让我们的一些 T4 模板工作时遇到了问题。 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.这些程序集是 .NET Core 3.1 库。 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.问题似乎是 Visual Studio 2019 中的 T4 引擎在 Framework 上运行,无法正确加载 Core 程序集。

When running the templates, I'm getting a ReflectionTypeLoadException .运行模板时,我收到ReflectionTypeLoadException After inspection of the exception, it becomes clear that the T4 engine can't find System.Runtime .检查异常后,很明显 T4 引擎找不到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.有一些解决方法(这里),但是当我应用它时,只会将问题移到下一个依赖项,即 NuGet 包。 So the workaround doesn't work.所以解决方法不起作用。

Is there any way to make T4 templates work with .NET Core 3.1 assemblies?有什么方法可以使 T4 模板与 .NET Core 3.1 程序集一起使用? Or is there any similar alternative to T4 templates that works with .NET Core 3.1?或者是否有与 .NET Core 3.1 一起使用的 T4 模板的类似替代方案?

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.因此,我开发了一个替代 T4 工具,它混合使用了 Visual Studio 的 .NET Framework T4 功能进行预处理和一个 .NET Core 可执行文件来执行模板代码。 It is available from GitHub .它可以从 GitHub 获得 It works well for my project and hopefully it can help some others too.它适用于我的项目,希望它也能帮助其他一些人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM