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

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

问题描述

  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

    您会得到这个异​​常:

    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)*'
    

    任何人在这里有一个关于它的更多信息? 这是错误的在一个ASP.NET MVC创建APP_ code(?)Web应用程序?

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

    推荐答案

    默认情况下,在Visual Studio的Web应用程序项目模型创建MVC项目。 APP_ code 主要是用于网站的模式。我建议您阅读关于它们之间的差异(另一个问题涉及此内容,它的也涵盖广泛在MSDN上)。如果您在Web应用程序项目源文件添加到 APP_ code ,Visual Studio将其编译为一个DLL(因为它包含在项目),并把它在 /箱。在运行时,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文件?否则,很可能,你实例化类是在源文件中的生成操作(右键单击文件,单击属性)设置为内容它告诉Visual Studio中跳过它在构建过程中(在这种情况下,你将无法引用它在其他的.cs文件,这些文件外 APP_ code 但你会能够用它在视图中,因为它只会来生活在运行时)。如果生成操作是编译,你会得到一个错误。这个问题肯定不是特定于扩展方法。 Visual Studio中似乎是足够聪明,它在默认情况下为加入 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天全站免登陆