單一方法即擴展方法之間的調用是模糊的

[英]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)'

以下方法或屬性之間的調用不明確:'myignOU.Extension.GetTLD(string)'和'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

這是編譯錯誤頁面

6 个解决方案

#1


56  

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.

我有同樣的問題,但對我來說,它解決了從項目引用中刪除自己的項目的問題。 Resharper意外地添加了對同一項目的編譯二進制文件的引用。這樣我在我的項目中有兩次相同的擴展類。在構建期間,它無法區分擴展類的源版本或二進制版本。

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

所以基本上:檢查項目引用是否包含對自身的引用。

Answer provided by @shashwat is also a case.

@shashwat提供的答案也是一個案例。

#2


12  

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.

我正在嘗試Project Properties,References,bin,obj中的所有內容,但實際上沒有任何幫助。我只是嘗試了每個選項,並從上下文菜單中選擇了“轉換為Web應用程序”。雖然之前它是一個Web應用程序。它向我展示了一個警告,說.designer.cs文件將被添加到每個aspx文件中,我只需單擊確定。

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.

除了App_Code文件夾(所有這些類都是)之外,一切都保持不變,重命名為Old App_Code,我將構建項目。現在我在運行時沒有收到編譯錯誤。

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文件夾是針對網站項目的,直到這次WAP才對此我沒有任何問題。但我只是意識到我不應該在WAP中使用App_Code文件夾,因為這里討論的是wrox論壇(第1點)。作者在這里說過

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.

WAP不支持App_Code。 App_Code文件夾在運行時編譯; WAP中的所有代碼都是在編譯/開發時編譯的。因此,當您向WAP添加App_Code文件夾時。你最終得到了重復的代碼;例如,App_Code中定義的類也將出現在wap DLL中。修復很簡單:只需將文件夾命名為Classes或CodeFiles。

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

我在轉換之前嘗試過重命名,但那次它沒有用。

#3


2  

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.

我有同樣的問題。經過一些研究和實驗,我發現(無論出於何種原因)visual studio不喜歡App_Code文件夾中的擴展方法。我發現最簡單的解決方案就是更改文件夾的名稱。

#4


1  

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.

刪除它解決了問題。

#5


0  

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.

我的問題類似於shashwat所描述的。在我的情況下,我將代碼文件放在App_Code目錄中,但為了獲得intellisense,我將文件本身設置為compile,這可能意味着文件在運行時和編譯時都被編譯。我剛把文件移出App_Code。

#6


0  

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.

我在項目A中引用了項目A.錯誤地說。

Removeing helps =)

刪除幫助=)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/06/24/f9dbbbd86ad8edb19c05f750b1697b45.html



 
  © 2014-2022 ITdaan.com 联系我们: