Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 702
Gluip Avatar asked Jul 13 '11 09:07

Gluip


People also ask

Where call is ambiguous between the following methods?

The call is ambiguous between the following methods or properties: 'Assert. That(ActualValueDelegate, IResolveConstraint)' and 'Asser - Visual Studio Feedback.

How do you remove ambiguous reference in C#?

Solution 1 You can: 1) Remove the using statement for the one you are not interested in. 2) Fully qualify the object when you reference it, by prefixing "File" with either "Scripting." or "System.IO."


2 Answers

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.

like image 137
Carra Avatar answered Sep 21 '22 19:09

Carra


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.

like image 35
Justin Avatar answered Sep 18 '22 19:09

Justin