compiler build error : The call is ambiguous between the following methods or properties
Asked Answered
D

4

14

I am experiencing a strange compiler error with extension methods. I have an assembly which has an extension method like

public static class MyClass
{
    public static Bar GetBar(this Foo foo)
    {
        return new Bar();
    }
}

And elsewhere in the same assembly i do something like this

Foo foo = new Foo();
var bar = foo.GetBar();

When i clean and compile everything is OK. BUT once i make a small change (like an extra whitespace) in the assembly and build again I get an error like this:

Error 973 The call is ambiguous between the following methods or properties: 'MyNameSpace.MyClass.GetBar(Foo)' and 'MyNameSpace.MyClass.GetBar(Foo)'

Only after i clean the project I can build again. Is this a problem in the compiler using an old version of the assembly? Only work around I see now is to replace my extension methods with normal static methods.

Decedent answered 13/7, 2011 at 9:23 Comment(6)
well then its not really a small change thenSeveralty
It is.. just an enter or something to get the project to build againDecedent
What references does this project have? Is it possible that you are acidentally indirectly referencing the same assembly?Wivinah
That's it! Somehow the project was directly referencing itself. Removing the reference probably fixes the problem. Thanks!Decedent
What about when you have a Web Application project instead of a Web Site project? In that case, VS creates an assembly in the bin folder with the same name as the project. My project would build fine, but would complain about ambiguous methods when I run it. :(Setaceous
To answer my own comment; I had my code files in App_Code, and renaming the folder to 'Code' fixed it.Setaceous
W
4

Ok having done a bit of playing round I can reproduce a similar situation to this by adding a file reference to the output file - the first time round it builds successflly (as the reference is not used - you simply get a "reference not resolved" warning), however from this point on I see the "The call is ambiguous" error appear in the code editor.

For me however this doesn't prevent me from building the solution (I'm testing this using Visual Studio 2010), however the error does appear - maybe under slighly different circumstances, such as a different Visual Studio version, this would stop the project from compiling.

You could also engineer this same situation with post-build steps that copy the output assembly.

Wivinah answered 13/7, 2011 at 9:48 Comment(0)
R
27

Took me a while to figure this one out but Gluips comment is the right one, I'll add it here for easy reference:

That's it! Somehow the project was directly referencing itself. Removing the reference probably fixes the problem. Thanks!

This fixed it for me.

Replace answered 11/10, 2012 at 13:45 Comment(6)
Can you add more detail please?Dagny
Been a while since I've answered t his but I it's something like this: Dll A references B, B references C and C references A.Replace
Unfortunately ReSharper sometimes do this. :(Undone
For me, it wasn't A referencing B, B referencing C, C referencing A, but A referencing A itself. I removed this reference and then all is fine. I'm also using Resharper, so that's probably the source for the weirdness.Assurance
I experienced this recently - in my case, I found that the compiler warnings were useful, as they itemise the different locations where the ambiguous target method is defined.Shin
Well I did experience the same problem and I am not using resharper, but Devexpress code rush (not blaming it for this though)Mccormac
W
4

Ok having done a bit of playing round I can reproduce a similar situation to this by adding a file reference to the output file - the first time round it builds successflly (as the reference is not used - you simply get a "reference not resolved" warning), however from this point on I see the "The call is ambiguous" error appear in the code editor.

For me however this doesn't prevent me from building the solution (I'm testing this using Visual Studio 2010), however the error does appear - maybe under slighly different circumstances, such as a different Visual Studio version, this would stop the project from compiling.

You could also engineer this same situation with post-build steps that copy the output assembly.

Wivinah answered 13/7, 2011 at 9:48 Comment(0)
A
0

I can't explain such behavior, but you should implement such method as a static directly in Foo class.

Alodee answered 13/7, 2011 at 9:42 Comment(0)
I
0

I've had this behavior upon compiling with MSBuild v14:

  • building in VS 2015 worked just fine
  • building on the same machine by MSBuild worked also
  • building on a server machine with only MSBuild preinstalled failed

Installing the .NET Developer Pack (in my case Developer Pack for 4.5.2) solved the issue, even if the error message was misleading.

Imperceptible answered 12/2, 2021 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.