C# 调试错误“在未引用的程序集中定义了类型 'xx'”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1728562/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:19:21  来源:igfitidea点击:

Debugging error "The Type 'xx' is defined in an assembly that is not referenced"

c#visual-studio-2008debuggingassembly-resolution

提问by Abel

The full error is as follows:

完整的错误如下:

The type 'System.Windows.Forms.Control' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

类型“System.Windows.Forms.Control”是在未引用的程序集中定义的。您必须添加对程序集“System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的引用。

and it points at the very first statement (an Debug.Assert line) in the very first class in a library project that doesn't need System.Windows.Forms(or so I thought). I know how to solve it: add the mentioned reference. But how do I find out what library is causing this error, or better, what part of the code triggers using the WinForms library?

它指向不需要的库项目中第一个类中的第一个语句(一个 Debug.Assert 行)System.Windows.Forms(或者我认为)。我知道如何解决它:添加提到的参考。但是我如何找出导致此错误的库,或者更好的是,使用 WinForms 库触发代码的哪一部分?

Normally, you can add libraries that reference others, but you only need to add references to these others when they're actually used.

通常,您可以添加引用他人的库,但您只需要在实际使用时添加对这些其他人的引用。



EDIT: Alternative solution

编辑:替代解决方案

This or similar problems can also be resolved using the Binding Log Viewer Fuslogvw.exefrom Microsoft's Framework Tools. It shows all attempts and successes of assemblies your application binds to.

这个或类似的问题也可以使用微软框架工具中的绑定日志查看器 Fuslogvw.exe来解决。它显示您的应用程序绑定到的程序集的所有尝试和成功。

采纳答案by Chris

I suspect there's no line of your code that's causing this, since you say you aren't making use of the System.Windows.Forms types and the compiler error isn't pointing to a (useful) line of your code.

我怀疑没有一行代码导致了这种情况,因为你说你没有使用 System.Windows.Forms 类型,并且编译器错误没有指向你的代码的(有用的)行。

What I think is happening is that you're referencing a library which has a publicly-visible method or property that either returns a System.Windows.Forms.Control or takes one as a parameter. It doesn't matter whether you actually end up callingthat method/property, the fact that it's publically visible means that your own code has to be able to resolve all the types that the library is using. If the library only used System.Windows.Forms internally, you wouldn't be experiencing this.

我认为正在发生的事情是您正在引用一个库,该库具有公开可见的方法或属性,该方法或属性返回 System.Windows.Forms.Control 或将其作为参数。您是否真的最终调用该方法/属性并不重要,它是公开可见的这一事实意味着您自己的代码必须能够解析库正在使用的所有类型。如果库仅在内部使用 System.Windows.Forms ,您就不会遇到这种情况。

It also means just looking at the dependencies of the assemblies you're depending on may merely narrow down the list of suspects, since there could be some assemblies that depend on System.Windows.Forms internally (no problem) and the one troublemaking assembly that has a public parameter / return value of a type from the S.W.Forms assembly.

这也意味着仅查看您所依赖的程序集的依赖关系可能只会缩小可疑列表的范围,因为可能存在一些内部依赖 System.Windows.Forms 的程序集(没问题)和一个麻烦的程序集具有来自 SWForms 程序集的类型的公共参数/返回值。

My suggestion is you just set up an empty project without a reference to S.W.Forms, then add each of your dependencies in turn and try to compile after each one.

我的建议是,您只需设置一个不引用 SWForms 的空项目,然后依次添加每个依赖项并尝试在每个依赖项之后进行编译。

回答by dice

Use something like NDepend or Reflector or the Object Browser to check the dependencies of the assemblies you depend on.

使用诸如 NDepend 或 Reflector 或对象浏览器之类的工具来检查您所依赖的程序集的依赖关系。

I cannot think of any other way given the info above.

鉴于上述信息,我想不出任何其他方式。

回答by AnatB

I had the same error.

我有同样的错误。

The problem was that I used a reference to a project, which uses System.Windows.Forms inside.

问题是我使用了对一个项目的引用,该项目在内部使用 System.Windows.Forms。

The solutionis to add a reference to System.Windows.Forms also in your project.

解决方案是在您的项目中添加对 System.Windows.Forms 的引用。