以下方法或属性之间的调用不明确(错误??) [英] The call is ambiguous between the following methods or properties (bug??)

查看:30
本文介绍了以下方法或属性之间的调用不明确(错误??)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 创建一个新的 ASP.NET MVC Web申请
  2. 创建一个 ASP.NET App_Code文件夹
  3. 里面的新文件夹,创建一个类扩展方法.例如:

  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);
    }
}

  • 选择一个视图并尝试使用这个新的扩展方法

  • Choose a View and try to use this new Extension Method

    你会得到这个异常:

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

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

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

    推荐答案

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

    MVC projects created in Visual Studio use Web application project model by default. App_Code is mostly used by Web site model. I suggest reading about differences between them (another question covers this and it's also covered extensively on MSDN). If you add a source file to App_Code in 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_Code and 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 .aspx file, it'll fail to choose one.

    这两个(扩展方法和您正在实例化的类)是否在单个 .cs 文件中?否则,您正在实例化的类可能位于源文件中,Build Action(右键单击文件,单击属性)设置为 Content,它告诉 Visual Studio 跳过它在构建过程中(在这种情况下,您将无法在 App_Code 之外的其他 .cs 文件中引用它,但您将能够在视图中使用它,因为它)只会在运行时生效.)如果构建操作是编译,您将收到错误消息.这个问题绝对不是扩展方法特有的.Visual Studio 似乎足够聪明,可以将其默认设置为 Content,将源文件添加到 App_Code.

    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 Content which 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_Code but 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.

    这篇关于以下方法或属性之间的调用不明确(错误??)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆