C# 类型“System.IDisposable”是在未引用的程序集中定义的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12349189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type 'System.IDisposable' is defined in an assembly that is not referenced
提问by jiali passion
I created a Windows Form application in Visual Studio 2012 on Windows 8, and the target .NET framework is 4.5, and I want to use the Datagramsocket library of Windows SDK, so I changed the target OS version to 8, when I compile.
我在Windows 8上的Visual Studio 2012中创建了一个Windows Form应用程序,目标.NET框架是4.5,我想使用Windows SDK的Datagramsocket库,所以我在编译时将目标OS版本更改为8。
I am getting the following error:
我收到以下错误:
"Error 1 The type 'System.IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'."
“错误 1 类型 'System.IDisposable' 在未引用的程序集中定义。您必须添加对程序集 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 的引用。”
回答by Drew Noakes
It looks like your app is trying to load System.Runtimeversion 4.0, but that you don't have that version installed. You say you have .NET 4.5 installed, so you can provide some configuration to redirect assembly bindingsat runtime.
您的应用似乎正在尝试加载System.Runtime4.0 版,但您没有安装该版本。您说您安装了 .NET 4.5,因此您可以提供一些配置以在运行时重定向程序集绑定。
Try adding this to your App.configor Web.configfile:
尝试将其添加到您的App.config或Web.config文件中:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="4.0.0.0" newVersion="4.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
I haven't tested this exact scenario, but this configuration has helped me in similar situations.
我还没有测试过这个确切的场景,但是这个配置在类似的情况下帮助了我。
回答by Nathan
If you are targeting Windows 8, the MSDN documentationhas the following instructions:
如果您的目标是 Windows 8,MSDN 文档有以下说明:
That said, your desktop app can't consume much of anything from the Windows Runtime until you prepare your project with one essential reference. The Windows Runtime defines some standard classes and interfaces in
System.Runtime, such asIEnumerable, that are used throughout the Windows Runtime libraries. By default, your managed desktop app won't be able to find these types, and so you must manually reference System.Runtime before you can do anything meaningful with Windows Runtime classes. To create this manual reference:
- Navigate to your managed desktop app project in the Solution Explorer.
- Right-click the References node and click Add Reference.
- Click the Browse tab.
- Click Browse….
- Navigate to the System.Runtime.dll fa?ade. You can generally find this in a path similar to:
%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.dll
也就是说,您的桌面应用程序不能使用 Windows 运行时中的任何内容,除非您使用一个基本参考准备您的项目。Windows 运行时在 中定义了一些标准类和接口
System.Runtime,例如IEnumerable在整个 Windows 运行时库中使用的。默认情况下,您的托管桌面应用将无法找到这些类型,因此您必须手动引用 System.Runtime,然后才能对 Windows 运行时类执行任何有意义的操作。要创建此手册参考:
- 在解决方案资源管理器中导航到您的托管桌面应用项目。
- 右键单击引用节点,然后单击添加引用。
- 单击浏览选项卡。
- 单击浏览...。
- 导航到 System.Runtime.dll 外观。您通常可以在类似于以下内容的路径中找到它:
%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.dll
Hope that helps.
希望有帮助。
回答by Josh
If you run into this problem with 4.5.2 and Unity, Microsoft has issued a fix, KB article: http://support.microsoft.com/kb/2971005
如果您在 4.5.2 和 Unity 中遇到此问题,Microsoft 已发布修复程序,知识库文章:http: //support.microsoft.com/kb/2971005
回答by Chris Towles
While working with a TFS build Agent I kept seeing this error below but only on a Windows 2012 R2 server with 4.5.2 installed. None of our Visual Studio machine ever had the compile error.
在使用 TFS 构建代理时,我一直在下面看到此错误,但仅在安装了 4.5.2 的 Windows 2012 R2 服务器上看到。我们的 Visual Studio 机器都没有出现编译错误。
"Error 1 The type 'System.IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'."
“错误 1 类型 'System.IDisposable' 在未引用的程序集中定义。您必须添加对程序集 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 的引用。”
The cause as others have pointed out has to do with Portable Class Libraries and as others have pointed out. but as for a solution some recommend a reference to the System.Runtime.dllfacade in %ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.dllI disagree as the path and version may be different on other machines.
正如其他人指出的那样,原因与可移植类库有关,正如其他人所指出的那样。但至于解决方案System.Runtime.dll,%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.dll我不同意某些人推荐对外观的引用,因为其他机器上的路径和版本可能不同。
Instead the best solution I've found was to install the Windows SDKon the build machine.
相反,我发现的最佳解决方案是在构建机器上安装Windows SDK。
I found this solution from a post here hereand a really clear description as to why this problem occurs here
回答by sliderhouserules
As Josh mentions, I had this problem in a solution with a Web API project and several library projects, where one of the library projects was borking on build, with errors that said the Unity attributes weren't "valid" attributes, and then one of the errors said The type 'System.Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly System.Runtime.
正如 Josh 提到的,我在一个 Web API 项目和几个库项目的解决方案中遇到了这个问题,其中一个库项目在构建时出错,错误表明 Unity 属性不是“有效”属性,然后是一个的错误说 The type 'System.Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly System.Runtime.
The suggested "updated" 4.5.2 Developer Pack is what I already had installed, so that didn't help me. On a hunch I thought maybe it was just a version mismatch. So I looked at the properties of every project, and one of the very base libraries was targeting 4.5 while every other one was targeting 4.5.2. I changed that one to also target 4.5.2 and the errors went away.
建议的“更新” 4.5.2 Developer Pack 是我已经安装的,所以对我没有帮助。凭直觉,我认为这可能只是版本不匹配。因此,我查看了每个项目的属性,其中一个非常基础的库针对 4.5,而其他所有库都针对 4.5.2。我将那个更改为也针对 4.5.2,错误消失了。
回答by wezzix
I had this problem in some solutions on VS 2015, and even in the same solution on one workstation but not on another. The errors started appeared after changing .NET version to 4.6.
我在 VS 2015 的一些解决方案中遇到了这个问题,甚至在一个工作站上的同一个解决方案中也有这个问题,但在另一个工作站上没有。将 .NET 版本更改为 4.6 后开始出现错误。
The solution is simple: Close the solution and delete the hidden .vs folderin the same folder as the solution.
解决方法很简单:关闭解决方案,删除解决方案所在文件夹中隐藏的.vs文件夹。
Adding the missing references as suggested in other answers also solves the problem, but the error remains solved even after you remove the references again.
按照其他答案中的建议添加缺失的引用也可以解决问题,但即使您再次删除引用,错误仍然可以解决。
回答by Aleksei Mialkin
I had this error message in TeamCity after I added Unity 4.0.1 to the project that is targeting .NET Framework 4.6.1. I ended up copying missing folders in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework from my development machine to the build server. Both are running Windows 7. After that build succeeded.
将 Unity 4.0.1 添加到面向 .NET Framework 4.6.1 的项目后,TeamCity 中出现此错误消息。我最终将 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework 中丢失的文件夹从我的开发机器复制到构建服务器。两者都运行 Windows 7。之后构建成功。
回答by Christer
I just ran into this issue with Windows 10, TeamCity and VS2015:
我刚刚在 Windows 10、TeamCity 和 VS2015 上遇到了这个问题:
Symptoms:
症状:
Visual studio builds the project fine, but TeamCity struggles with compile issues.
Visual Studio 可以很好地构建项目,但 TeamCity 遇到了编译问题。
Reason:
原因:
Added a new project to existing solution - Forgot to change the TargetRuntimeVersion.
向现有解决方案添加了一个新项目 - 忘记更改 TargetRuntimeVersion。
Solution:
解决方案:
Unload project
卸载项目
Edit .csproj file for that project
编辑该项目的 .csproj 文件
Change:
改变:
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
To whatever you other projects are, or whatever is installed on the server.
无论您的其他项目是什么,或者服务器上安装了什么。
For me it was:
对我来说是:
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
Hope this helps anyone else experiencing this.
希望这可以帮助其他任何遇到此问题的人。
回答by Julius Depulla
Based on TFS 2015 Build Server
基于 TFS 2015 构建服务器
I do not know if this error message is generic and the answers/solution may be unique for each unique error. But I had similar error the type 'System.IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
我不知道此错误消息是否是通用的,并且每个唯一错误的答案/解决方案可能都是唯一的。但我有类似的错误the type 'System.IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
I realised that the project was using Microsoft.Practices.Unityand had interface IUnityContainerwhich implements IDisposable. It turned out that the TFS Build Definition NuGet Installer Build Step was not configured, which is responsible for restoring Microsoft.Practices.UnityNuGet package on the TFS Build Server. If you come across this error, please check your TFS Build Definition and point the NuGet Installer Build Step Path to your solution full path, including the .slnextension
我意识到该项目正在使用Microsoft.Practices.Unity并且具有IUnityContainer实现IDisposable. 原来是没有配置TFS Build Definition NuGet Installer Build Step,它负责恢复Microsoft.Practices.UnityTFS Build Server上的NuGet包。如果您遇到此错误,请检查您的 TFS 构建定义并将 NuGet 安装程序构建步骤路径指向您的解决方案完整路径,包括 . sln延期


