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

9

我有一个 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 Standard 库进行定位。我不确定这是否适用于我,因为我的整个解决方案都只涉及 .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 项目。 - Aluan Haddad
好的,我的代码都不是netstandard的。所以我可以花功夫把它们转为netstandard,但我认为也应该有一个适当的netcore解决方案。 - Dillon Drobena
1
如果必要的话,我可能会走这条路。从技术上讲,只有我的一个项目“需要”成为一个netcore项目。 - Dillon Drobena
如果是这样,你将获得可移植性作为副作用。这可能非常值得。不过他们应该在框架中修复这个问题... - Aluan Haddad
1
EF 6 将在 3.0 版本中被移植到 Core 平台。 - Aluan Haddad
显示剩余3条评论
1个回答

17

我找到了一个解决办法,可以解决 System.Runtime dll 错误。

将这个 bindingRedirect 放在 C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\15.0_29f8d23a\devenv.exe.config 文件的 <configuration> -> <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


2
快速更新...他们没有为 .Net Core 3.0 修复它。 - Dillon Drobena
1
有没有适用于多人项目的通用解决方案?意思是,我的团队有200多人将与相同的c# .sln进行交互,并且让他们修改其devenv配置不是可扩展的解决方案。这是否可以在app.config文件中配置? - Kevin Cohen
1
@petrosmm 我发现重复显示的4行代码,将System.Runtime替换为System.Collections或任何缺失的程序集。 - David0337
1
对于VS2022 Net6.0,这对我没有用。然而,我有拥有我正在尝试使用的DLL源代码的自由。我将被消耗的DLL项目降级到了.Net框架4.8。编译被消耗的项目。现在我的T4可以加载我的DLL并使用其中的内容。 - Micah Epps
2
针对VS2022 .Net 6.0,我在这里找到了一个可行的解决方案/解决方法:https://dev59.com/kEQFtIcB2Jgan1znI-3b#60778223。它涉及到一个需要安装到VS中的扩展程序,可以解决T4的错误部分。源代码在GitHub上。 - M-Peror
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接