【问题标题】:T4 Template Could not load file or assembly 'System.Runtime, Version = 4.2.0.0'T4 模板无法加载文件或程序集“System.Runtime,版本 = 4.2.0.0”
【发布时间】:2018-07-27 03:17:11
【问题描述】:

所以我有一个 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 类库项目。我已经尝试在这个特定的库中添加 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()

【问题讨论】:

  • 您可以从核心项目中引用 netstandard 项目。
  • 好吧,我的都不是网络标准。所以我可以通过努力使它们成为这样,但我不明白为什么也不会有合适的 netcore 解决方案
  • 同意应该有办法,但转换项目并不困难或耗时
  • 如果是这样,我可能会走这条路。从技术上讲,我的项目中只有 1 个“需要”成为 netcore 项目
  • EF 6 将在 3.0 中移植到 Core

标签: c# visual-studio-2017 asp.net-core-2.0 t4


【解决方案1】:

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

把这个 bindingRedirect 放在 C:\Users\&lt;user&gt;\AppData\Local\Microsoft\VisualStudio\15.0_29f8d23a\devenv.exe.config 里面 &lt;configuration&gt; -> &lt;runtime&gt; -> &lt;assemblyBinding&gt; 哪里是所有其他 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

【讨论】:

  • 非常感谢!我不敢相信我刚才看到了这个答案。最近试了一下,确实解决了我的问题。我最近获得了 Visual Studios 2019,所以我希望他们能修复它。希望 .Net Core 3.0 能够按原样工作。
  • 快速更新...他们没有为 .Net Core 3.0 修复它
  • 这对我有用,但我被一个非常相似的 System.Collections 卡住了......想法?
  • 多人项目有通用解决方案吗?这意味着,我的团队中有 200 多人将与同一个 c# .sln 进行交互,让他们修改他们的 devenv 配置并不是一个可扩展的解决方案。这是可以在 app.config 文件中配置的吗?
  • @petrosmm 我发现重复显示的 4 行,将 System.Runtime 替换为 System.Collections 或消息说丢失的任何程序集
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多