C# 为什么我不能引用我的类库?

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

Why can't I reference my class library?

c#visual-studio-2008resharperreference

提问by Brandon

I have a solution that contains a website and a class library in Visual Studio 2008.

我有一个包含网站和 Visual Studio 2008 类库的解决方案。

I then have another web site project outside of the solution that needs to reference the class library. I right click the Bin folder or Project and select Add Reference, then select my Class Library Project, it adds the 15 or so DLLs that the class library needs to the websites bin folder, but none of the .cs files recognize the using statements.

然后我有另一个需要引用类库的解决方案之外的网站项目。我右键单击 Bin 文件夹或项目并选择添加引用,然后选择我的类库项目,它将类库需要的 15 个左右的 DLL 添加到网站 bin 文件夹中,但没有一个 .cs 文件识别 using 语句。

using MyLibrary.MyFolder;

It says that it can't resolve it, and ReSharper just says it can be safely removed since it's not being used.

它说它无法解决它,ReSharper 只是说它可以安全地删除,因为它没有被使用。

ReSharper can recognize that it needs the reference and suggests that it "Reference MyLibrary and use MyFolder". I'm not sure why it's suggesting I add a reference I already have. When I go with the suggestion, I get the error

ReSharper 可以识别出它需要引用并建议它“引用 MyLibrary 并使用 MyFolder”。我不知道为什么它建议我添加一个我已经拥有的参考。当我接受建议时,我收到错误

"Failed to reference module. Probably, reference will produce circular dependencies between projects."

“无法引用模块。可能,引用会在项目之间产生循环依赖。”

I've tried going to the websites property pages and removing all the references and re-adding them, but it gives the same errors. Any ideas why this isn't working?

我试过转到网站属性页面并删除所有引用并重新添加它们,但它给出了相同的错误。任何想法为什么这不起作用?

采纳答案by Brandon

I found how to fix this issue (for me at least). Why it worked, I'm not sure, but it did. (I just tried against a second website that was having the same problem and the following solution worked for that as well).

我找到了如何解决这个问题(至少对我而言)。为什么它有效,我不确定,但确实如此。(我只是尝试了第二个遇到相同问题的网站,以下解决方案也适用)。

I tried the normal cleaning of the projects and rebuilding, shutting down all my Visual Studio instances and restarting them, even tried restarting my computer.

我尝试了对项目的正常清理和重建,关闭所有 Visual Studio 实例并重新启动它们,甚至尝试重新启动我的计算机。

What actually worked was opening up the project in Visual Studio, closing all the open tabs, and then shutting it down.

真正有效的是在 Visual Studio 中打开项目,关闭所有打开的选项卡,然后将其关闭。

Before I had left the tabs open because I didn't think it mattered (and I hardly ever close the tabs I'm using).

在我打开选项卡之前,因为我认为这无关紧要(而且我几乎从不关闭我正在使用的选项卡)。

回答by Vilx-

Since they are both in the same solution, instead of adding a reference to the DLL, add a reference to the class library project itself (the Add Reference dialog will have a tab for this).

由于它们都在同一个解决方案中,而不是添加对 DLL 的引用,而是添加对类库项目本身的引用(“添加引用”对话框将为此提供一个选项卡)。

Ahh, it's a different solution. Missed that. How about you try instead of adding a reference to the project addding a reference to the compiled DLL of your class library. The Add Reference dialog has a Browse tab which does this.

啊,这是一个不同的解决方案。错过了。您如何尝试而不是添加对项目的引用,而是添加对类库的已编译 DLL 的引用。添加引用对话框有一个浏览选项卡,可以执行此操作。

回答by KP.

This sounds like a similar issue with ReSharper:

这听起来与 ReSharper 类似:

http://www.jetbrains.net/devnet/thread/275827

http://www.jetbrains.net/devnet/thread/275827

According to one user in the thread forcing a build fixes the issue (CTRL+Shift+B) after the first build..

根据线程中的一个用户强制构建修复了第一次构建后的问题 (CTRL+Shift+B)。

Sounds like an issue with ReSharper specifically in their case.. Have you tried building regardless of the warnings and possible false errors?

听起来像是 ReSharper 的问题,特别是在他们的情况下。您是否尝试过在不考虑警告和可能的错误错误的情况下进行构建?

回答by Fedor Steeman

I had a similar problems where VS would sometimes build and sometimes not. After some searching and attempts I discovered that I had an ambiguous reference to a class with the same name in different libraries ('FileManager'). The project that would not build were my Unit Tests that reference all modules in my solution. Enforcing the reference to a specific module sorted things out for me.

我有一个类似的问题,其中 VS 有时会构建,有时不会。经过一些搜索和尝试,我发现我对不同库('FileManager')中同名的类有一个不明确的引用。无法构建的项目是我的单元测试,它引用了我的解决方案中的所有模块。强制引用特定模块为我解决了问题。

My point is: Rather than blaming ReSharper or VS, it may be a good idea to double check if there really isn't some kind of circular reference somehow. More than often, classes with the same names in different modules could cause confusion and is often a symptom of bad design (like in my case).

我的观点是:与其责怪 ReSharper 或 VS,不如仔细检查是否真的没有某种循环引用。通常,不同模块中具有相同名称的类可能会导致混淆,并且通常是糟糕设计的症状(例如我的情况)。

回答by Marijn

I had a similar issue in VS 2010, when creating a test project for an MVC 2 application. The symptoms were identical.

在为 MVC 2 应用程序创建测试项目时,我在 VS 2010 中遇到了类似的问题。症状完全相同。

The message from ReSharper was somewhat misleading. For a moment I completely ignored ReSharper and did it the "manual VS way":

来自 ReSharper 的消息有些误导。有那么一刻,我完全忽略了 ReSharper,而是采用了“手动 VS 方式”:

  1. I cleaned the solution.
  2. I manually added the reference to the MVC project.
  3. I manually added the using directives.
  4. ctrl-shift-b
  1. 我清洗了溶液。
  2. 我手动添加了对 MVC 项目的引用。
  3. 我手动添加了 using 指令。
  4. ctrl-shift-b

At this stage I got a compilation error: I should have referenced the System.Web.Mvcassembly in my test project (sigh). Adding this reference causes the project to compile. The ReSharper issues remain, but the ReSharper test runner works.

在这个阶段,我遇到了一个编译错误:我应该System.Web.Mvc在我的测试项目中引用了程序集(叹气)。添加此引用会导致项目编译。ReSharper 问题仍然存在,但 ReSharper 测试运行程序有效。

When I restart VS, the ReSharper errors are gone too. I'm not sure if the restart is required - simply closing the .cs file might be enough.

当我重新启动 VS 时,ReSharper 错误也消失了。我不确定是否需要重新启动 - 只需关闭 .cs 文件就足够了。

From now on, when I see the ReSharper message

从现在开始,当我看到 ReSharper 消息时

Failed to reference module. Probably, reference will produce circular dependencies between projects.

无法引用模块。可能,引用会在项目之间产生循环依赖。

I'll read

我会读

Failed to reference module. Probably, reference will produce circular dependencies between projects, or you are missing some references to dependencies of the reference's dependencies.

无法引用模块。可能,引用会在项目之间产生循环依赖,或者您缺少对引用依赖项的依赖项的一些引用。

回答by KeithS

Another possible fix that just worked for me:

另一个可能对我有用的修复方法:

If you have Assembly A, which references Assembly B, both of which reference a non-project (external) assembly X, and Assembly B's code will not recognize that you have referenced X, then try the following steps in order:

如果您的程序集 A 引用了程序集 B,两者都引用了非项目(外部)程序集 X,并且程序集 B 的代码无法识别您引用了 X,请按顺序尝试以下步骤:

  • Drop reference to X from BOTH A and B
  • Recreate reference to X in B
  • Recreate reference to X in A
  • 从 A 和 B 中删除对 X 的引用
  • 在 B 中重新创建对 X 的引用
  • 在 A 中重新创建对 X 的引用

Apparently, VS will not recognize a reference to an external assembly in a project that is a dependency of another project that already references the external. By setting up the references again from the ground up, you overcome this. It's just very odd.

显然,VS 不会识别一个项目中对外部程序集的引用,该引用是另一个已经引用外部程序集的项目的依赖项。通过从头开始重新设置引用,您可以克服这个问题。这很奇怪。

回答by MacGyver

If you're referencing assemblies for projects that are in the same solution, add a Project reference (using the "Projects" tab) rather than browsing for the dll in the \bin\Debug (or \bin\Release) folder (using the "Browse" tab). See screen shot below. Only browse for the assembly/dll file if it's considered an external assembly.

如果要为同一解决方案中的项目引用程序集,请添加项目引用(使用“项目”选项卡),而不是浏览 \bin\Debug(或 \bin\Release)文件夹中的 dll(使用“浏览”选项卡)。请参阅下面的屏幕截图。如果它被视为外部程序集,则仅浏览程序集/dll 文件。

enter image description here

在此处输入图片说明

回答by Alex

I deleted *.csproj.user ( resharper file) of my project, then, close all tabs and reopen it. After that I was able to compile my project and there was no resharper warnings.

我删除了我的项目的 *.csproj.user(resharper 文件),然后关闭所有选项卡并重新打开它。之后,我能够编译我的项目,并且没有重新锐化警告。

回答by CindyH

Also, check that the new solution's projects run against a compatible framework to the project you're trying to include. I was trying to include a reference to a 4.0 project in a 3.5 project.

此外,检查新解决方案的项目是否针对您尝试包含的项目的兼容框架运行。我试图在 3.5 项目中包含对 4.0 项目的引用。

回答by Rocky

I had a similar problem, will all my references being buggered up by Resharper - The solution which worked for me is to clear the Resharper Cache and then restarting VS

我遇到了类似的问题,Resharper 是否会干扰我的所有引用 - 对我有用的解决方案是清除 Resharper 缓存,然后重新启动 VS

tools->options->resharper->options-> general-> click the clear caches button and restart VS

工具->选项->resharper->选项->常规->点击清除缓存按钮并重启VS