C# 以下方法或属性之间的调用不明确(错误??)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1282636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:38:44  来源:igfitidea点击:

The call is ambiguous between the following methods or properties (bug??)

c#.netasp.net-mvc.net-3.5

提问by Cleiton

  1. Create a new ASP.NET MVC Web Application
  2. Create an ASP.NET App_Code Folder
  3. Inside the new folder, create a class with an Extension Method. For example:

    static public class BugMVCExtension
    {
        public static int ToInt(this string str)
        {
            return Convert.ToInt32(str);
        }
    }
    
  4. Choose a View and try to use this new Extension Method

  1. 创建一个新的 ASP.NET MVC Web 应用程序
  2. 创建 ASP.NET App_Code 文件夹
  3. 在新文件夹中,使用扩展方法创建一个类。例如:

    static public class BugMVCExtension
    {
        public static int ToInt(this string str)
        {
            return Convert.ToInt32(str);
        }
    }
    
  4. 选择一个视图并尝试使用这个新的扩展方法

You will get this Exception:

你会得到这个异常:

CS0121: The call is ambiguous between the following methods or properties:
'*MvcApplication1.App_code.BugMVCExtentions.ToInt(string)*' and
'*MvcApplication1.App_code.BugMVCExtentions.ToInt(string)*'

Anyone here has more information about it? Is it wrong to create an App_code in an ASP.NET MVC(?) Web Applications?

这里有人有更多关于它的信息吗?在 ASP.NET MVC(?) Web 应用程序中创建 App_code 是错误的吗?

采纳答案by Mehrdad Afshari

MVC projects created in Visual Studio use Web application project model by default. App_Codeis mostly used by Web site model. I suggest reading about differences between them (another question covers thisand it's also covered extensively on MSDN). If you add a source file to App_Codein a Web application project, Visual Studio will compile it to a DLL (since it's included in the project) and puts it in /bin. At run time, the ASP.NET compiler sees App_Codeand tries to compile the source in a different assembly. As a consequence, two separate classes with identical names will exist in two different assemblies and when the ASP.NET parser tries to compile the .aspxfile, it'll fail to choose one.

在 Visual Studio 中创建的 MVC 项目默认使用 Web 应用程序项目模型。App_Code主要用于网站模型。我建议阅读它们之间的差异(另一个问题涵盖了这一点,并且在 MSDN 上也有广泛的介绍)。如果App_Code在 Web 应用程序项目中添加源文件,Visual Studio 会将其编译为 DLL(因为它包含在项目中)并将其放入/bin. 在运行时,ASP.NET 编译器会看到App_Code并尝试在不同的程序集中编译源代码。因此,两个具有相同名称的独立类将存在于两个不同的程序集中,并且当 ASP.NET 解析器尝试编译该.aspx文件时,它将无法选择一个。

Update:

更新:

Are those two (extension method and the class you're instantiating) in a single .cs file? Otherwise, probably, the class you're instantiating is in a source file with Build Action(right click on file, click properties) set to Contentwhich tells Visual Studio to skip it in the build process (in that case, you won't be able to reference it in other .cs files that are outside App_Codebut you'll be able to use it in the view since it'll only come to life at run time.) If the build action is Compile, you'll get an error. The issue is definitely not specific to extension methods. Visual Studio seems to be smart enough to set it to Content by default for source files added to App_Code.

这两个(扩展方法和您正在实例化的类)是否在单个 .cs 文件中?否则,您正在实例化的类可能位于一个源文件中,其中Build Action(右键单击文件,单击属性)设置为Content,它告诉 Visual Studio 在构建过程中跳过它(在这种情况下,您不会能够在外部的其他 .cs 文件中引用它,App_Code但您将能够在视图中使用它,因为它只会在运行时出现。)如果构建操作是Compile,您将获得错误。这个问题绝对不是扩展方法特有的。对于添加到App_Code.

回答by user1385616

I solved by placing in the file of extension of the full namespace of the folder.

我通过将文件夹的完整命名空间的扩展名放在文件中来解决。

namespace Helpers {

命名空间助手{

namespace xxx.yyy.zzz.Helpers {

命名空间 xxx.yyy.zzz.Helpers {

回答by Ivan

I solved the problem by placing the .cs file outside the App_Code folder, in the root of the Web Application.

我通过将 .cs 文件放在 Web 应用程序根目录中的 App_Code 文件夹之外解决了这个问题。

回答by Jaime Enrique Espinosa Reyes

Do not use the app_code folder.

不要使用 app_code 文件夹。

Use any other folder name. IE. appCode or ApplicationsCode.

使用任何其他文件夹名称。IE。appCode 或 ApplicationsCode。

The name in the folder implies some compilation at runtime that leads to have duplicated code.

文件夹中的名称暗示了在运行时进行的一些编译,这会导致代码重复。