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

前端 未结 6 936
悲&欢浪女
悲&欢浪女 2021-02-12 04:06

I have an extension method like

public static class Extension
{
    public static string GetTLD(this string str)
    {
        var host = new System.Uri(str).Hos         


        
相关标签:
6条回答
  • 2021-02-12 04:27

    I had the same problem. After some research and experimenting, I found that (for whatever reason) visual studio doesn't like extension methods in the App_Code folder. Easiest solution I found was to just change the name of the folder.

    0 讨论(0)
  • 2021-02-12 04:28

    I had the same situation. I had a custom grid control for which there was an extension method. The method was only declared in one place. The control had been in use in multiple places throughout my solution with no problems. Then one day, I copied a control that implemented the custom grid control by literally copying and pasting within the same project. Then I just renamed the properties as appropriate. After that the solution wouldn't build quoting the same error as above. I suspect the copy and paste added to the project a reference to itself.

    Deleting it resolved the problem.

    0 讨论(0)
  • 2021-02-12 04:28

    My issue was similar to what shashwat describes. In my case I put the code file in the App_Code dir, but in order to get intellisense, I set the file itself to compile which likely means that the file was getting compiled at both runtime and compile time. I just moved the file out of App_Code.

    0 讨论(0)
  • 2021-02-12 04:29

    I have found this solution in the comments above. And it helped me.

    I have the reference to project A in the project A. By mistake.

    Removeing helps =)

    0 讨论(0)
  • 2021-02-12 04:31

    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.

    0 讨论(0)
  • 2021-02-12 04:34

    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.

    0 讨论(0)
提交回复
热议问题