C# 错误:对已定义的类型声明的引用,但找不到

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

Error: Reference to type claims it is defined, but it could not be found

c#visual-studioassemblies

提问by Tim Rudnevsky

I have a solution with 3 projects:

我有一个包含 3 个项目的解决方案:

  • ParsersBase, that define an interface IParseRule
  • ParsersLibrary, that have a reference to ParsersBase and define a class HtmlImageUrlParseRule : IParseRule
  • ParsersLibraryTest, that have a reference to ParsersBase and ParsersLibrary and define a test class with some test methods
  • ParsersBase,定义了一个接口 IParseRule
  • ParsersLibrary,引用 ParsersBase 并定义一个类 HtmlImageUrlParseRule : IParseRule
  • ParsersLibraryTest,有对 ParsersBase 和 ParsersLibrary 的引用,并用一些测试方法定义一个测试类

When I'm trying to build it, I get a warning:

当我尝试构建它时,我收到一条警告:

Reference to type 'AVSoft.ParsersBase.IParseRule' claims it is defined in 'c:\Users\Tim\Dropbox\projects\Image Downloader\ParsersLibrary\bin\Debug\ParsersLibrary.dll', but it could not be found

对类型“AVSoft.ParsersBase.IParseRule”的引用声称它在“c:\Users\Tim\Dropbox\projects\Image Downloader\ParsersLibrary\bin\Debug\ParsersLibrary.dll”中定义,但找不到

Why VS is trying to find AVSoft.ParsersBase.IParseRulein ParsersLibrary.dll? ParsersLibraryTest has a reference to ParsersBase, it just doesn't make any sense.

为什么 VS 试图AVSoft.ParsersBase.IParseRule在 ParsersLibrary.dll 中查找?ParsersLibraryTest 有一个对 ParsersBase 的引用,它只是没有任何意义。

采纳答案by Tim Rudnevsky

It was my fault, I had a ParsersLibrary project at the start and then renamed it to ParsersBase, but I didn't rename an assembly name, then I added a ParsersLibrary project again.

是我的错,我一开始有一个ParsersLibrary项目,然后重命名为ParsersBase,但是我没有重命名一个程序集名称,然后我又添加了一个ParsersLibrary项目。

So, two projects had the same assembly name and it's not very good, is it? :) Assemblies overlap each other, so I have this error.

所以,两个项目有相同的程序集名称,这不是很好,是吗?:) 程序集相互重叠,所以我有这个错误。

回答by Slugart

ParsersLibraryTest needs to reference ParsersBase. The second part of the error should read "You must add a reference to assembly 'ParsersBase..."

ParsersLibraryTest 需要引用 ParsersBase。错误的第二部分应为“您必须添加对程序集'ParsersBase...的引用”

回答by wiseowl2828

I had a similar problem. The site was running a cached version of the dll and not the one I had deployed to the bin directory. I cleared the temporary asp.net folder contents and this solved the issue.

我有一个类似的问题。该站点运行的是 dll 的缓存版本,而不是我部署到 bin 目录的版本。我清除了临时 asp.net 文件夹内容,这解决了问题。

回答by Chris Marisic

I hit this exception today. The problem in my case was I had some.package v2.1installed in my host and some.package v2.3installed in other projects. Update-Packageon the host project to v2.3fixed the issue.

我今天遇到了这个例外。我的问题是我已经some.package v2.1安装在我的主机上并some.package v2.3安装在其他项目中。Update-Package在宿主项目上v2.3解决了这个问题。

回答by Rob Church

Another way this could happen is if you're using several NuGet packages where one, probably central, package has been updated but some additional functionality packages haven't been.

另一种可能发生的情况是,如果您使用多个 NuGet 包,其中一个包(可能是中央包)已更新,但某些附加功能包尚未更新。

To work through my recent example - the error was "Reference to type 'ConsumerSubscriptionConfigurator<>' claims it is defined in 'MassTransit', but it could not be found". This is because we had updated MassTransitfrom 2 to 3, but we had not updated some of the other optional packages (MassTransit.log4netand MassTransit.Autofac) to the same version number. It appears as if assembly redirection had kept everything working until we tried to use one more additional feature.

为了解决我最近的例子 - 错误是“引用类型'ConsumerSubscriptionConfigurator<>'声称它在'MassTransit'中定义,但找不到”。这是因为我们已将MassTransit从 2更新为 3,但我们尚未将其他一些可选包(MassTransit.log4netMassTransit.Autofac)更新为相同的版本号。看起来好像程序集重定向使一切正常,直到我们尝试使用另一个附加功能。

回答by Scott Simontis

This error seems to cover a variety of scenarios. In my case, closing and re-opening Visual Studio was the trick. After restarting Visual Studio, I was able to build the project as expected.

这个错误似乎涵盖了多种情况。就我而言,关闭并重新打开 Visual Studio 是诀窍。重新启动 Visual Studio 后,我能够按预期构建项目。

回答by Alexandra Sh

I had the similar problem: Reference to type 'Func<>' claims it is defined in 'mscorlib', but it could not be found. The problem was following: my solution had Target Framework = 3.5 and I added a reference to Microsoft.Practices.Prism v 4.0 which is built against the framework 4.0.

我遇到了类似的问题:引用类型 'Func<>' 声称它是在 'mscorlib' 中定义的,但找不到。问题如下:我的解决方案有 Target Framework = 3.5,我添加了对 Microsoft.Practices.Prism v 4.0 的引用,它是针对框架 4.0 构建的。

After changing target framework to 4.0 it worked

将目标框架更改为 4.0 后,它起作用了

回答by Kyle Barnes

I tried all of the above answers but none resolved my issue.

我尝试了上述所有答案,但都没有解决我的问题。

In the end, I checked in my latest code (GIT), then recloned the repository in a different location.

最后,我签入了我的最新代码 (GIT),然后将存储库重新克隆到了不同的位置。

Not ideal, but at least problem solved.

不理想,但至少问题解决了。

回答by Robetto

I had the similar problem: Reference to type 'Func<>' claims it is defined in 'mscorlib', but it could not be found.I have a lib of .Net 4 that was referenced by a .Net 3.5 program. After upgrading both to 4.61 it worked.

我遇到了类似的问题:引用类型 'Func<>' 声称它是在 'mscorlib' 中定义的,但找不到。我有一个由 .Net 3.5 程序引用的 .Net 4 库。将两者都升级到 4.61 后,它起作用了。

Seems like Func<T>is missing in .Net 3.5 and just upgrading that sample app would have been enough.

似乎Func<T>在 .Net 3.5 中缺少,只需升级该示例应用程序就足够了。

Further exp: Someone had added a signature in the library project (.Net 4) that uses a Func<T>parameter. But the sample program (3.5) already existed and ran fine so far. But at time of recompilation a dependency of a dependency clashed. Because mscorelib-3.5 had been already loaded. All happens at compilation time.

进一步说明:有人在使用Func<T>参数的库项目(.Net 4)中添加了一个签名 。但是示例程序 (3.5) 已经存在并且到目前为止运行良好。但是在重新编译时,依赖项的依赖项发生了冲突。因为已经加载了 mscorelib-3.5。一切都发生在编译时。

回答by BornToCode

The only way I could overcome this error was to force uninstall of all nuget packages related and then reinstalling them. Sad but true.

我能克服这个错误的唯一方法是强制卸载所有相关的 nuget 包,然后重新安装它们。悲伤但真实。