g.pickardou
g.pickardou

Reputation: 35812

C# compiler: CS0121: The call is ambiguous between the following methods or properties

This is the craziest what I've seen since a Fody plugin ruined my assembly by emitting invalid code and control flow varied random at runtime... No Fody this time.

Facts:

Any ideas?

enter image description here

and here is the single one method:

namespace ComiCalc.Data
{

    using System;
    using System.Data.Entity.Validation;
    using PluralTouch.DataAccess;
    // TODO: Move to PluralTouch
    public static class ExceptionExtensions
    {
        public static string GetMessage2(this Exception exception)
        {

            var message = exception.Message;
            if (exception is DbEntityValidationException)
            {
                message = ((DbEntityValidationException) exception).DbEntityValidationResultToString();
            }
            return message;
        }
    }
}

enter image description here

Upvotes: 11

Views: 59008

Answers (11)

Andrew Roberts
Andrew Roberts

Reputation: 744

I encountered this error when using Shared Projects in Visual Studio. My shared project was included as a reference by project A. Project B then referenced project A. I mistakenly added the shared project to project B, leading to two instances of each of the shared files in the project. I then saw this error for a string extension method in one of the shared files where it was called from another shared file.

Upvotes: 0

Neftaly Perez
Neftaly Perez

Reputation: 1

In my case I just have add "path" to the code!!! hope that helps error CS0121

Upvotes: -2

I had the same issue while migrating from .NET3.1 to .NET6.0. I got my issue fixed by updating the reference package "System.Interactive" to the latest version (6.0.1 in my case) in csproj file.

Upvotes: 0

Tim Lovell-Smith
Tim Lovell-Smith

Reputation: 16115

In my case, I hit the same problem [impossible resolution conflicts between a single method and itself] because I used C# 8.0 and its shiny new features to build three projects that depend upon each other, transitively, like this:

  • Project C depends on project B.
  • Project B depends on project A.
  • Project C calls APIs from types in project B that inherit types from project A. Some of those APIs have nullable reference type parameters.

Also, I had nullability checking enabled only for project A, and completely disabled for projects B and C.

The fix for me was to enable nullable annotations in project B (I did this without also enabling warnings).

I speculate that this fixes the problem by ensuring there weren't two completely different compiler modes (nullable annotated on/off) reprocessing the same interface definition, while building two projects (A and B), and then up putting conflicting nullability annotations on their respective outputs.

Upvotes: 0

Ayub
Ayub

Reputation: 2375

In my case, Resharper had created a new file named Annotations1.cs file. I just deleted it and the problem solved. Maybe you need delete debug / release / obj and clean / rebuild solution.

enter image description here

Upvotes: 1

LiliumCandidum
LiliumCandidum

Reputation: 61

In my case I had the "publish" folder within my project folder. Removing the "publish" folder solved it for me.

Upvotes: 0

jaimenino
jaimenino

Reputation: 64

I had two exact classes created by a DB tool, because was executed 2 times with different parameters. I search by the class name and remove the newer, after that everything goes OK.

Upvotes: 0

Renat
Renat

Reputation: 8962

Got the same error because my project referenced to a dll, and at the same time had a transitive dependency to the same dll but at different file path.

Spotted it using extra logging during the build (Tools -> Options -> Build and Run ->output verbosity to Detailed or Diagnostic) by searching for dependency dll name: in Task "Csc" there were two occurrences of /reference with the dll

Fix was to point references to the same dll path

Upvotes: 1

serdar
serdar

Reputation: 454

Delete bin folder > open project > build solution.

Upvotes: 6

Erdogan
Erdogan

Reputation: 990

I created a new project and moved all files except 'bin', 'references', 'obj' and boom. It works like charm..

Upvotes: 0

Jcl
Jcl

Reputation: 28272

Make sure you don't reference the output binary in your project references (i.e., the project references itself). This has happened to me in the past with Resharper (the addition of the output binary to the project references), so the extension method is both in the source and in the binary reference.

Upvotes: 30

Related Questions