Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The call is ambiguous between single method i.e extension method

I have an extension method like

public static class Extension {     public static string GetTLD(this string str)     {         var host = new System.Uri(str).Host;         int index = host.LastIndexOf('.'), last = 3;         while (index >= last - 3)         {             last = index;             index = host.LastIndexOf('.', last - 1);         }         var domain = host.Substring(index + 1);         return domain;     } } 

And I am calling this like

string domain = "." + _url.GetTLD(); 

I am getting no error at building and clean build.
But I am getting compilation error at run time error saying

The call is ambiguous between the following methods or properties: 'myIGNOU.Extension.GetTLD(string)' and 'myIGNOU.Extension.GetTLD(string)'

I swear that I don't have this extension method placed any where else too in the project. Why I am getting this error only at run time..?

But if I delete this method then I am getting error at build time not not at run time. Everything works fine without the code of this method.

Here is compilation error page

like image 884
shashwat Avatar asked Jun 24 '13 19:06

shashwat


People also ask

How can the ambiguous extension method be resolved?

Ambiguity can be resolved if concurrent namespaces which have extension methods with same name, are included at different levels (most inner included namespace will have priority).

How do you call an extension method?

To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.

What is extension method in MVC?

What is extension method? Extension methods in C# are methods applied to some existing class and they look like regular instance methods. This way we can "extend" existing classes we cannot change. Perhaps the best example of extension methods are HtmlHelper extensions used in ASP.NET MVC.


2 Answers

I had the same problem but for me it solved the problem to remove the own project from the project references. Resharper accidentally added a reference to the compiled binary of the same project. That way I had the same extension class 2 times within my project. During building it couldn't distinguish between the source-version or the binary-version of the extension class.

So basically: Check your project references if it contains a reference to itself.

Answer provided by @shashwat is also a case.

like image 175
Danielku15 Avatar answered Oct 14 '22 00:10

Danielku15


This is not a real solution and I can't explain how did it work but it worked.

I was trying everything in Project Properties, References, bin, obj but nothing helped me really. And I was just trying every option and chose 'Convert to Web Application' from the context menu. Although It was a Web Application itself before. It showed me a warning saying that .designer.cs files will be added to each aspx file and I just clicked OK.

Everything remained same except App_Code folder (where all these classes were) was renamed to Old App_Code and I'd build the project. And now I am not getting compilation error at run time.

I know App_Code folder are meant to Website Project and I was having no issue with this till this time in WAP. But I just realize I should not have App_Code folder in WAP as it is discussed here in a wrox forum (1st point). Author has said here that

App_Code is not supported in a WAP. The App_Code folder is compiled at run-time; all code in a WAP is compiled at compile / development time. So, when you add an App_Code folder to a WAP. you end up with duplicate code; for example, a class defined in App_Code will also show up in the wap DLL. The fix is easy: just name the folder something else like Classes or CodeFiles.

I have tried it renaming too before conversion but that time it did not work.

like image 31
shashwat Avatar answered Oct 13 '22 22:10

shashwat