[c#] Why can't I reference my class library?

I have a solution that contains a website and a class library in 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.

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 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

"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?

This question is related to c# visual-studio-2008 resharper reference

The answer is


One possibility is that the target .NET Framework version of the class library is higher than that of the project.


I faced this problem, and I solved it by closing visual studio, reopening visual studio, cleaning and rebuilding the solution. This worked for me.


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.


Unfortunately the only thing that worked for me was completely deleting and recreating the class library project, after having temporarily copied the class files in it elsewhere. Only then would the ASP.Net web project recognise the using statements that referred to the class library project. This was with Visual Studio 2010, not using ReSharper.


I had stumbled upon a similar issue recently. I am working in Visual Studio 2015 with Resharper Ultimate 2016.1.2. I was trying to add a new class to my code base while trying to reference a class from another assembly but Resharper would throw an error for that package.

With some help of a co-worker, I figured out that the referenced class existed in the global namespace and wasn't accessible from the new class since it was hidden by another entity of same name that existed in current namespace.

Adding a 'global::' keyword before the required namespace helped me to reference the class I was actually looking for. More details on this can be found on the page listed below:

https://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx


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.


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.

enter image description here


If both projects are contained within the same solution, it will be more apropiate if you add the reference for the project you need, not its compiled dll.

enter image description here


I had this problem. It took me ages to figure out. I had people over my shoulder to help. We rebuilt, cleaned and restarted Visual studio and this didn't fix it. We removed and re-added the references...

All to no avail.... Until!

The solution to my problem was that my class declaration was spelt incorrectly.

Before you start judging me harshly, allow me to explain why it wasn't stupid, and also why this mistake could be made by even the most intelligent of programmers.

Since the mistake was early on in the name, it wasn't appearing in the intellisense class listing when I began typing.

e.g.

Class name: Message.cs

Declaration:

public class Massage
{
    //code here

}

At a glance and in a small font, Massage looks identical to Message.

Typing M listed too many classes, so I typed e, which didn't appear in the mistyped version, which gave the impression that the class wasn't being picked up by the compiler.


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:

  • Drop reference to X from BOTH A and B
  • Recreate reference to X in B
  • Recreate reference to X in A

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.


If using TFS, performing a Get latest (recursive) doesn't always work. Instead, I force a get latest by clicking Source control => Get specific version then clicking both boxes. This tends to work.

enter image description here

If it still doesn't work then deleting the suo file (usually found in the same place as the solution) forces visual studio to get all the files from the source (and subsequently rebuild the suo file).

If that doesn't work then try closing all your open files and closing Visual studio. When you next open Visual studio it should be fixed. There is a resharper bug that is resolved this way.


This sounds like a similar issue with ReSharper:

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..

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


I had similar issue. What worked for me is that I had added wrong Class Library from visual studio. I added by using the search feature of visual studio.

What I needed to do was Add New Project > Visual C# > Class Library. And this newly added class library is the right one and can now be added as reference to any project.


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

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


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.

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).


You may forget to add reference the class library which you needed to import.

Right click the class library which you want to import in (which contains multiple imported class libraries), -->Add->Reference(Select Projects->Solution->select the class library which you want to import from->OK)


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).

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.


After confirming the same version of asp.net was being used. I removed the project. cleaned the solution and re-added the project. this is what worked for me.


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

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

  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

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

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.

From now on, when I see the ReSharper message

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.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to visual-studio-2008

Convert Text to Uppercase while typing in Text box SQL Server r2 installation error .. update Visual Studio 2008 to SP1 What is and how to fix System.TypeInitializationException error? Error LNK2019: Unresolved External Symbol in Visual Studio download and install visual studio 2008 Git in Visual Studio - add existing project? Visual Studio can't 'see' my included header files How to insert Records in Database using C# language? If statements for Checkboxes LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

Examples related to resharper

Tests not running in Test Explorer Test method is inconclusive: Test wasn't run. Error? Visual Studio displaying errors even if projects build Keyboard shortcuts are not active in Visual Studio with Resharper installed Where do I mark a lambda expression async? What does CultureInfo.InvariantCulture mean? Handling warning for possible multiple enumeration of IEnumerable Why should I use var instead of a type? How do I generate a constructor from class fields using Visual Studio (and/or ReSharper)? What are some alternatives to ReSharper?

Examples related to reference

Method Call Chaining; returning a pointer vs a reference? When to create variables (memory management) Reference to non-static member function must be called Cannot find reference 'xxx' in __init__.py - Python / Pycharm c++ "Incomplete type not allowed" error accessing class reference information (Circular dependency with forward declaration) C++ initial value of reference to non-const must be an lvalue Dependent DLL is not getting copied to the build output folder in Visual Studio How to write to error log file in PHP How to reference Microsoft.Office.Interop.Excel dll? Linker Error C++ "undefined reference "