T4 模板无法加载文件或程序集 'System.Runtime, Version = 4.2.0.0'

T4 Template Could not load file or assembly 'System.Runtime, Version = 4.2.0.0'

所以我有一个 T4 模板,我在设计时尝试 运行,但它一直给我以下错误。

Running transformation: System.IO.FileNotFoundException: Could not load 
file or assembly 'System.Runtime, Version=4.2.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system 
cannot find the file specified.
File name: 'System.Runtime, Version=4.2.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a'

我的 Visual Studios 解决方案中包含 10 个项目,所有这些项目都针对 .Net Core 2.0 框架。我的 T4 模板目前看起来是这样的:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="Services.Resources.DataTransferObjects.Infrastructures" #>
<#@ import namespace="System.Collections.Generic" #> 
<#@ assembly name="$(TargetDir)Services.dll" #>


<#@ output extension=".cs" #>
public class AdminDTO
{
        <#var editableObjs = Assembly
            .GetAssembly(typeof(GenericEditable<>))
            .GetTypes()
            .Where(p => p.BaseType != null && p.BaseType.IsGenericType && p.BaseType.GetGenericTypeDefinition() == (typeof(GenericEditable<>)))
            .ToList();
        #>
}

目前我只需要我在模板中引用的程序集,它是一个 .Net Core 2.0 class 库项目。我试过在这个特定的库中添加 System.Runtime.dll 引用,但它似乎没有任何区别。

我读过其他几个与此类似的问题,通常看来 .Net Core 似乎与 T4 模板有问题,但似乎大多数人的解决方案都是针对 .Net 标准库。我不确定这是否适用于我,因为我的整个解决方案只涉及 .Net Core 项目。

编辑

我将所有项目更改为以 .Net Standard 2.0 而不是 .Net Core 为目标,这解决了我最初的问题,但现在我看到了这个错误:

System.Reflection.ReflectionTypeLoadException: Unable to load one or 
more of the requested types. Retrieve the LoaderExceptions property for 
more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()

我找到了 System.Runtime dll 错误的解决方法。

将此 bindingRedirect 放在 <configuration> 内的 C:\Users\<user>\AppData\Local\Microsoft\VisualStudio.0_29f8d23a\devenv.exe.config -> <runtime> -> <assemblyBinding> 其他所有 bindingRedirect 的

<dependentAssembly>
  <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
  <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>

这是 Visual Studio 或 T4 模板引擎中的错误:https://developercommunity.visualstudio.com/content/problem/358905/filenotfoundexception-systemruntime-version4210-wh.html