Nonexistent ambiguous reference - c#

In a Visual Studio 2010 project, we had two identically named classes in two different namespaces that are both commonly included in ViewModels throughout the application. One such class has since been deprecated and removed, but I'm getting an ambiguous reference error when directly referring to the remaining class, even though the old class no longer exists.
We basically have something like this:
using OurNamespace.UI.Common;
using OurNamespace.SomewhereElse;
// *snip*
SomeClass.SomeMethod();
Once upon a time, both of the above namespaces had a SomeClass, but we deleted the one in OurNamespace.UI.Common. However, when building, we get the following error:
'SomeClass' is an ambiguous reference between 'OurNamespace.UI.Common.SomeClass' and 'OurNameSpace.SomewhereElse.SomeClass'
I've already tried cleaning the solution and rebuilding as suggested in answers to this ambigous reference question, only to continue to see the error. What's still lurking behind that makes it think the deleted class still exists for purposes of an ambiguous reference? Even IntelliSense knows there's only one now.

If you look at your project's references (via the References section of the Solution Explorer window), you can right-click the reference and select View in Object Browser.
This allows you to investigate the referenced assemblies to see if the offending class is still lurking in any of them.

There's still a reference to the namespace that the old class used to be in (because it has other things in use)
I think your problem is with the reference to the dll that used to contain the deleted class.
Simply remove the reference and re-add it. It should solve the issue.

Super old thread, but in VS2017 I had this happen and had to restart VS to get it fixed after I moved files between namespaces.

Related

The type 'class' in 'directory\class.aspx.cs' conflicts with the imported type 'class' in 'app'. Using type defined in 'directory\class.aspx.cs'

I've been fighting with this issue all day. VS2019, these warnings started coming up after switching the application to use Windows Authentication. No matter what markup I focus on, I receive this error on each and every one. This is a solution containing one project.
Some things I have tried:
Clean, rebuild, restart.
Checked references to see if there was any cyclical references, none.
No 'App_Code' folder used.
Cleaned up registry items regarding VS2019.
Delete bin and obj.
Deleted all references, re added each.
Cleared nuget cache.
Made sure that 'class' wasn't duplicated anywhere.
Fully qualified namespace in .cs and .aspx.
Delete temporary aspx files.
Just hoping someone else has some suggestions. My last option would be to create the project from scratch. I understand these warnings aren't breaking, however they lead me to believe there is something wrong behind the scenes.
Don't use the word "class" as your Class name. It's the reserved keyword for C#

Namespace x does not exist in the <project> namespace, even if the reference is added to the project

After merging my development branch with release branch in TFS(Visual Studio),
I am seeing this error in many places.
But, the reference is already added to the project.
After, I remove the reference and add it again, the error goes away. The references are of other projects in the solution.
Is there any precaution I can take to avoid manually removing/adding
several references again?
Is there a way to refresh(for lack of a better word) the added
references to resolve these type of errors?
Appreciate your time and attention.

CS0436: Type conflicts with the imported type

I am including an instance of the same source files in multiple assemblies using the Add As Link option. I specifically need to include an instance of the same source within these assemblies because it is responsible for licence validation which must occur internally to the assembly. Performing licence calls across module boundaries could introduce a security risk.
Some of the projects in my solution that include the code depend on other modules that also include it, resulting in warning CS0436:
"The type [type] in [licence.cs full path] conflicts with the imported
type [LicenceClass] in [dependency project also including licence.cs].
Using the type defined in [licence.cs full path]".
I have tried declaring a class alias, but the definitions internal to licence.cs cause the same warning. In the alias, there must be a reference to the duplicated class name which causes the same warning.
I know it is bad practice to duplicate source between assemblies, but it is intentional in this case. I would rather keep a central instance that each assembly links to rather than a dedicated instance with renamed classes to avoid the warnings.
The workaround I have is simply to ignore the warning using a #pragma. Is there a more elegant solution?
It is worth noting that another way to get such warnings is by simply setting a project in visual studio to reference itself: References -> Solution -> etc etc (how I figured this gem out is left as an exercise to the reader ...)
Visual Studio will happily comply, only to throw a wall of warnings of the type described by OP during build, which is to be expected (upon reflection) since every single class etc is getting defined twice.
The only time conflicts occur is when two dependent classes include the same class. There are two workarounds:
Disable the warning in classes that cause CS0436:
#pragma warning disable 0436
Have a separate instance of the class, uniquely named in each client project (undesirable from a maintenance point of view).
EDIT: There is also a solution: do what Mark suggests below, and mark duplicate classes internal.
I had a web application I converted from ASP.NET 3.5 to 4.5 when I moved to VS2015. I started seeing this as a warning, but the solution would still compile. There were no circular references, and cleaning the solution and deleting the bin and obj folders didn't help.
It turns out that VS2015 wasn't happy with some of my classes in the App_Code folder. The classes in here had the same namespace as the rest of the web pages in the parent folder. Once I moved these classes out of the App_Code folder and to the top level of the web application, the warnings went away.
In .NET Core you can also disable the warning in project.json:
{
"buildOptions":
{
"nowarn":
[
"CS0436"
]
}
}
I had this error but not with 2 different classes!
Each new class where in conflict with itself, so obviously I had that CS0436 Error.
After some struggling found out that it was about Mirror Asset that I was using in my multiplayer Unity project. Mirror somehow was including every new class that I make (and inherit from NetworkBehavior).
My external editor was VSCode (visual studio code, solution might also apply to visual studio).
Solution
in
Edit / Preferences / External tools / "Generate .csproj files for:"
I started testing different settings, and this worked for me:
(Not sure if the exact settings work for all, but not having the right files in project, leads to this error. like my case.)
Click Regenerate project files and restart Unity and VSCode after applying these settings (or the setting that suits your project).
I've met such a case when removed some source files temporarily and restored them back later. It happens that IDE (Rider in my case) tries to restore the classes so when they were missing it just added the reference to the resulting exe. Evidently, when I restored the files, they look as duplicate.
The reference IDE inserted looks like this and it's enough to just remove it to fix:
<Reference Include="AppName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>bin\x86\Debug\AppName.exe</HintPath>
</Reference>

For C# resources, why is Properties is undefined?

When I try try to access a string property in one of my C# project resources, I get the following error:
'ORG.PRJ.MOD.MyClass2' does not contain a definition for 'Properties'
The code that produces the error is:
string s = MyClass2.Properties.Resources.TestString2;
The really bizarre thing is that another project in my solution (using MyClass and TestString) with exactly the same setup does NOT produce the error and works like a champ. Here is the background and all the things I have tried.
Both are plain "Class Library C#" projects in VS2008.
I created the resource strings via right-click->Properties for each project in the solution tree, then selecting the "Resources" tab. Then I just entered a "TestString" in one, and a "TestString2" in the other.
For the project that works, if I type MyClass. - the IDE IntelliSense tells me that "Properties" is an available member (but that is the ONLY member it shows me). For the project that doesn't work, the available members are "Equals" and "ReferenceEquals" (it does NOT give me "Properties"). This seems to be the biggest clue that SOMETHING is different.
Thinking my project files got corrupted, I completely created both projects again from scratch. I deleted all bin, obj, and Properties folders, deleted all Resources.resx and Resources.Designer.cs files, deleted all .csproj, .csproj.user, .sln, and .suo files. For BOTH projects. Then I started up VS2008 again and used File->New->"Project From Existing Code..." to create new projects. Then I added the resources in exactly the same way for both projects (per Step 2 above). Same results.
I have performed a 'diff' on the corresponding files between the two projects (Resources.resx, Resources.Designer.cs, MyProj.csproj). Nothing looks different other than what I would expect (class names and string names differ between them).
I've googled it to death. Based on how bizarre this feels, there's no doubt I've done something insanely stupid (see https://stackoverflow.com/questions/58640/great-programming-quotes/756768#756768).
The Properties static class is accessible via the default namespace for your project. Now, given that it's a class library, that might be Class2 (or perhaps that there might be a naming clash, ie. by having a Class2.Class2), but something tells me that that is a class in your library, not the namespace (which would produce the error you describe).

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?
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.
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.
What actually worked was opening up the project in Visual Studio, closing all the open tabs, and then shutting it down.
Before I had left the tabs open because I didn't think it mattered (and I hardly ever close the tabs I'm using).
One possibility is that the target .NET Framework version of the class library is higher than that of the 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).
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?
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.
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.
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.
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.
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":
I cleaned the solution.
I manually added the reference to the MVC project.
I manually added the using directives.
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.
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.
I faced this problem, and I solved it by closing visual studio, reopening visual studio, cleaning and rebuilding the solution. This worked for me.
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.
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.
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
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.
I had the exact same problem.
I tried closing Visual Studio numerous times, I tried deleting and adding a new class library.Checked if I had the right version, if I had referenced it in the target project. Nothing worked.
Then I thought maybe, just maybe I cannot reference the library because it was empty... and that was it.
As soon as I added a class to it the problem was fixed. So if you have tried everything and you are close to losing your sanity. Just try adding something to the class library.
the solution for was just adding the access modifier
my class didnt have any access modifier then i just added public and it worked!
Contracts class library:
namespace Contracts
{
public interface ILoggerManager
{
void LogInfo(string message);
void LogWarn(string message);
void LogDebug(string message);
void LogError(string message);
}
}
Logger service class library:
using Contracts;
using NLog;
public class LoggerManager : ILoggerManager
{
private static NLog.ILogger logger = LogManager.GetCurrentClassLogger();
public LoggerManager()
{
}
}
I tried various solutions for this issue. An old WebForms application refused to acknowledge the existence of a library even though a reference existed.
Oddly, what worked was to add and reference the class library in Visual Studio 2022, safe the project, then reopen in an earlier version of Visual Studio.
My solution was simple, but I'll share it in case someone else has the same issue and finds this question by googling like I did.
It turns out that the most recent build of the supporting DLL was done in Debug mode, and my code was looking at the Release version of the DLL. I rebuilt the DLL in Release mode and all is working properly.
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 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.
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)

Categories