T4 模板无法加载文件或程序集“System.Runtime,版本 = 4.2.0.0"

T4 Template Could not load file or assembly #39;System.Runtime, Version = 4.2.0.0#39;(T4 模板无法加载文件或程序集“System.Runtime,版本 = 4.2.0.0)

本文介绍了T4 模板无法加载文件或程序集“System.Runtime,版本 = 4.2.0.0"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have a T4 Template I'm trying to run at design-time, but it keeps giving me the following error.

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 模板目前如下所示:

My Visual Studios solution has 10 projects contained within it, all of which target .Net Core 2.0 framework. My T4 template presently looks as such:

<#@ 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 类库项目.我已经尝试在这个特定的库中添加 System.Runtime.dll 引用,但它似乎没有任何区别.

At the moment I only need the assembly I've referenced in the template, which is a .Net Core 2.0 class library project. I've tried adding the System.Runtime.dll reference in this particular library, but it doesn't appear to make any difference.

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

I've read several other issues similar to this, and it generally seems that .Net Core seems to have issues with T4 Templates, but it appeared most peoples' solutions were to target a .Net Standard library. I'm not sure this is applicable for me, as my entire solution only involves .Net Core projects.

编辑

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

I changed all my projects to target .Net Standard 2.0 instead of .Net Core, and that fixed my initial problem, but now I see this error:

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 错误的解决方法.

I found a workaround for the System.Runtime dll error.

将此 bindingRedirect 放入 C:UsersAppDataLocalMicrosoftVisualStudio15.0_29f8d23adevenv.exe.config -> <runtime> -> <assemblyBinding> 其他的 bindingRedirect 在哪里

Put this bindingRedirect inside the C:Users<user>AppDataLocalMicrosoftVisualStudio15.0_29f8d23adevenv.exe.config inside <configuration> -> <runtime> -> <assemblyBinding> where are all the others bindingRedirect's

<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

It's a bug in Visual Studio or T4 Template Engine: https://developercommunity.visualstudio.com/content/problem/358905/filenotfoundexception-systemruntime-version4210-wh.html

这篇关于T4 模板无法加载文件或程序集“System.Runtime,版本 = 4.2.0.0"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:T4 模板无法加载文件或程序集“System.Runtime,版本 = 4.2.0.0"

基础教程推荐